亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

一個可以一直滾動的ImageView(可做視差效果)

系統 1644 0




    
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>

  

一個可以一直滾動的ImageView(可做視差效果)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 中文字幕一二三区乱码老 | www.欧美xxx| 欧美毛片在线播放观看 | 92自拍视频 | 亚洲成人在线免费视频 | 欧美成人精品第一区二区三区 | 黄色在线免费看 | 日本a视频在线 | 成人在线a | 黄色理伦 | 免费在线看a | 亚洲精品第一页中文字幕 | 99爱在线精品视频免费观看9 | 国产欧美中文字幕 | 亚洲精品在线不卡 | 日本毛片在线 | 日本 毛片基地-亚洲 | 国产亚洲精品热视频在线观看 | 国产成人一级片 | 成年人午夜影院 | 四虎w345com| 欧美视频在线一区二区三区 | 一级骚片超级骚在线观看 | 欧美日韩中文在线 | 久久国产在线观看 | 亚洲欧美第一 | 国产亚洲福利精品一区 | 久久蝌蚪 | 国产精品亚洲一区二区麻豆 | 草久视频在线 | 热久久99影院 | 天天色天天射综合网 | 久久精品午夜视频 | 亚洲另类 专区 欧美 制服 | 在线观看深夜观看网站免费 | 国产精品日本一区二区不卡视频 | 久久99亚洲精品久久久久99 | 模特精品一区二区三区 | 天天看片夜夜爽 | 一本大道加勒比久久综合 | 久久亚洲不卡一区二区 |