一个可以一直滚动的ImageView(可做视差效果)

来源:互联网 发布:手机淘宝视频怎么保存 编辑:程序博客网 时间:2024/04/27 14:52



Java代码  收藏代码
  1. import android.content.Context;  
  2. import android.content.res.TypedArray;  
  3. import android.graphics.Bitmap;  
  4. import android.graphics.BitmapFactory;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Rect;  
  7. import android.util.AttributeSet;  
  8. import android.view.View;  
  9.   
  10. import static java.lang.Math.abs;  
  11. import static java.lang.Math.floor;  
  12.   
  13. /** 
  14.  * Created by thijs on 08-06-15. 
  15.  */  
  16. public class ScrollingImageView extends View {  
  17.     private final int speed;  
  18.     private final Bitmap bitmap;  
  19.   
  20.     private Rect clipBounds = new Rect();  
  21.     private int offset = 0;  
  22.   
  23.     private boolean isStarted;  
  24.   
  25.     public ScrollingImageView(Context context, AttributeSet attrs) {  
  26.         super(context, attrs);  
  27.         TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ParallaxView, 00);  
  28.         try {  
  29.             speed = ta.getDimensionPixelSize(R.styleable.ParallaxView_speed, 10);  
  30.             bitmap = BitmapFactory.decodeResource(getResources(), ta.getResourceId(R.styleable.ParallaxView_src, 0));  
  31.         } finally {  
  32.             ta.recycle();  
  33.         }  
  34.         start();  
  35.     }  
  36.   
  37.     @Override  
  38.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  39.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  40.         setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), bitmap.getHeight());  
  41.     }  
  42.   
  43.     @Override  
  44.     public void onDraw(Canvas canvas) {  
  45.         super.onDraw(canvas);  
  46.         if (canvas == null) {  
  47.             return;  
  48.         }  
  49.   
  50.         canvas.getClipBounds(clipBounds);  
  51.   
  52.         int normalizedOffset = offset;  
  53.         int layerWidth = bitmap.getWidth();  
  54.         if (offset < -layerWidth) {  
  55.             offset += (int) (floor(abs(normalizedOffset) / (float) layerWidth) * layerWidth);  
  56.         }  
  57.   
  58.         int left = offset;  
  59.         while (left < clipBounds.width()) {  
  60.             canvas.drawBitmap(bitmap, getBitmapLeft(layerWidth, left), 0null);  
  61.             left += layerWidth;  
  62.         }  
  63.   
  64.         if (isStarted) {  
  65.             offset -= speed;  
  66.             postInvalidateOnAnimation();  
  67.         }  
  68.     }  
  69.   
  70.     private float getBitmapLeft(int layerWidth, int left) {  
  71.         float bitmapLeft = left;  
  72.         if (speed < 0) {  
  73.             bitmapLeft = clipBounds.width() - layerWidth - left;  
  74.         }  
  75.         return bitmapLeft;  
  76.     }  
  77.   
  78.     /** 
  79.      * Start the animation 
  80.      */  
  81.     public void start() {  
  82.         if (!isStarted) {  
  83.             isStarted = true;  
  84.             postInvalidateOnAnimation();  
  85.         }  
  86.     }  
  87.   
  88.     /** 
  89.      * Stop the animation 
  90.      */  
  91.     public void stop() {  
  92.         if (isStarted) {  
  93.             isStarted = false;  
  94.             invalidate();  
  95.         }  
  96.     }  
  97. }  


attr.xml
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="ParallaxView">  
  4.         <attr name="speed" format="dimension" />  
  5.         <attr name="src" format="reference" />  
  6.     </declare-styleable>  
  7. </resources>  


usage:
Xml代码  收藏代码
  1. <com.....ScrollingImageView  
  2. xmlns:app="http://schemas.android.com/apk/res-auto"  
  3.     android:id="@+id/scrolling_background"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="wrap_content"  
  6.     app:speed="1dp"  
  7.     app:src="@drawable/scrolling_background" />  


Java代码  收藏代码
  1. ScrollingImageView scrollingBackground = (ScrollingImageView) loader.findViewById(R.id.scrolling_background);  
  2. scrollingBackground.stop();  
  3. scrollingBackground.start();  


Parallax effect:
只要设置不同的滚动背景速率就可以达到视差效果
Java代码  收藏代码
  1. <FrameLayout  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="wrap_content">  
  4.   
  5.   <com......ScrollingImageView  
  6.       android:id="@+id/scrolling_background"  
  7.       android:layout_width="match_parent"  
  8.       android:layout_height="wrap_content"  
  9.       app:speed="1dp"  
  10.       app:src="@drawable/scrolling_background" />  
  11.   
  12.   <com.....ScrollingImageView  
  13.       android:id="@+id/scrolling_foreground"  
  14.       android:layout_width="match_parent"  
  15.       android:layout_height="wrap_content"  
  16.       app:speed="2.5dp"  
  17.       app:src="@drawable/scrolling_foreground" />  
  18. </FrameLayout>  
  • 查看图片附件
0 0