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

一個可以一直滾動的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條評論
主站蜘蛛池模板: 欧美在线观看视频网站 | 色吧五月婷婷 | 欧美日韩亚洲国产一区二区综合 | 久久午夜影视 | 一区二区视频在线观看 | xxx中国bbbwww| 久久免费成人 | 真人一级一级特黄高清毛片 | 一级a毛片免费观看 | 久久久久综合中文字幕 | 特级按摩一级毛片 | 国产精品91视频 | 色综合天天射 | 成人禁在线观看午夜亚洲 | 日韩综合久久 | 日本免费久久 | 久草在线视频免费 | 久久精品国内偷自一区 | 狠狠躁夜夜躁人人爽天天段 | 99爱在线精品视频免费观看9 | 日本欧美久久久久免费播放网 | 国产精品人成在线播放新网站 | 亚洲视频免费在线看 | 欧美久久久久久久一区二区三区 | 日韩亚洲一区二区三区 | 欧美xxxb| 亚洲黄网视频 | 97成人精品 | 国产伦精品一区二区免费 | 国产区成人综合色在线 | 酒色网站 | 亚洲在线免费视频 | 久久青| 天天做天天爱天天一爽一毛片 | 一级片在线免费看 | 天天碰天天操 | 大学生一一级毛片在线播放 | 毛片免费全部播放一级 | 五月婷婷综合色 | 国产系列欧美系列日韩系列在线 | 日本成本人在线观看免费视频 |