一个可以一直滚动的ImageView(可做视差效果)
来源:互联网 发布:手机淘宝视频怎么保存 编辑:程序博客网 时间:2024/04/27 14:52
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Rect;
- import android.util.AttributeSet;
- import android.view.View;
- import static java.lang.Math.abs;
- import static java.lang.Math.floor;
- /**
- * Created by thijs on 08-06-15.
- */
- public class ScrollingImageView extends View {
- private final int speed;
- private final Bitmap bitmap;
- private Rect clipBounds = new Rect();
- private int offset = 0;
- private boolean isStarted;
- public ScrollingImageView(Context context, AttributeSet attrs) {
- super(context, attrs);
- TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ParallaxView, 0, 0);
- try {
- speed = ta.getDimensionPixelSize(R.styleable.ParallaxView_speed, 10);
- bitmap = BitmapFactory.decodeResource(getResources(), ta.getResourceId(R.styleable.ParallaxView_src, 0));
- } finally {
- ta.recycle();
- }
- start();
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), bitmap.getHeight());
- }
- @Override
- public void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- if (canvas == null) {
- return;
- }
- canvas.getClipBounds(clipBounds);
- int normalizedOffset = offset;
- int layerWidth = bitmap.getWidth();
- if (offset < -layerWidth) {
- offset += (int) (floor(abs(normalizedOffset) / (float) layerWidth) * layerWidth);
- }
- int left = offset;
- while (left < clipBounds.width()) {
- canvas.drawBitmap(bitmap, getBitmapLeft(layerWidth, left), 0, null);
- left += layerWidth;
- }
- if (isStarted) {
- offset -= speed;
- postInvalidateOnAnimation();
- }
- }
- private float getBitmapLeft(int layerWidth, int left) {
- float bitmapLeft = left;
- if (speed < 0) {
- bitmapLeft = clipBounds.width() - layerWidth - left;
- }
- return bitmapLeft;
- }
- /**
- * Start the animation
- */
- public void start() {
- if (!isStarted) {
- isStarted = true;
- postInvalidateOnAnimation();
- }
- }
- /**
- * Stop the animation
- */
- public void stop() {
- if (isStarted) {
- isStarted = false;
- invalidate();
- }
- }
- }
attr.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="ParallaxView">
- <attr name="speed" format="dimension" />
- <attr name="src" format="reference" />
- </declare-styleable>
- </resources>
usage:
- <com.....ScrollingImageView
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:id="@+id/scrolling_background"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:speed="1dp"
- app:src="@drawable/scrolling_background" />
- ScrollingImageView scrollingBackground = (ScrollingImageView) loader.findViewById(R.id.scrolling_background);
- scrollingBackground.stop();
- scrollingBackground.start();
Parallax effect:
只要设置不同的滚动背景速率就可以达到视差效果
- <FrameLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <com......ScrollingImageView
- android:id="@+id/scrolling_background"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:speed="1dp"
- app:src="@drawable/scrolling_background" />
- <com.....ScrollingImageView
- android:id="@+id/scrolling_foreground"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:speed="2.5dp"
- app:src="@drawable/scrolling_foreground" />
- </FrameLayout>
0 0
- 一个可以一直滚动的ImageView(可做视差效果)
- 一个可以一直滚动的ImageView(可做视差效果)
- 一个可以一直滚动的ImageView
- 利用background-attachment做视差滚动效果
- jquery实现的视差滚动教程(视差大背景效果)
- CSS3滚动视差效果的制作技巧
- 页面滚动视差效果的实现
- CSS3滚动视差效果的制作技巧
- 视差滚动效果
- android图片视差滚动效果
- css视差滚动效果1
- 使用javascript开发的视差滚动效果的云彩
- 简短的Swift-tableHeaderImage的视差滚动效果
- 分享14个超酷的视差滚动效果网站
- 40个视差滚动效果网站的新技术应用
- 40个视差滚动效果网站的新技术应用
- 超棒的视差滚动效果javascript类库 - Jarallax
- 视差滚动(Parallax Scrolling)效果的原理和实现
- WAP页上传图片
- 1509.Rails
- **ubuntu**小技巧
- iOS AutoLayout 百分比布局
- Python定时备份mysql数据库并把备份邮件发送
- 一个可以一直滚动的ImageView(可做视差效果)
- [最新版]MJRefresh解析与详细使用指导
- 加快首屏渲染速度(一)——抽取critical CSS
- 感知机 代码实现
- #436 – 给Border加上立体阴影(Using a Drop Shadow with a Border)
- mybatis 调用存储过程 返回游标 实例
- [MySQL] 索引类型
- Native Client - Application - File I/O
- 点击超链接不跳转,不刷新页面