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

一個可以一直滾動的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條評論
主站蜘蛛池模板: 97国产视频 | 狠狠色噜噜狠狠狠狠奇米777 | 伊人婷婷色 | 成人免费精品视频 | 国产剧情一区二区三区 | 亚洲乱码一区二区三区在线观看 | 亚洲狠狠97婷婷综合久久久久 | 久久久不卡国产精品一区二区 | 欧美精品xxx | 国产色婷婷精品综合在线观看 | 婷婷伊人五月 | 91视频免费看 | 仑乱高清在线一级播放 | 美女性色| 亚洲精品久久99久久一区 | 一本大道香蕉久在线不卡视频 | 久久91精品久久久久久水蜜桃 | 伊人色综合久久天天爱 | 国产亚洲精品一区二区三区 | 久久国内精品自在自线400部o | 日韩欧美国产偷亚洲清高 | 老司机激情影院 | 日本一级毛片视频无遮挡免费 | 亚洲欧美成人综合久久久 | 国产中文字幕在线免费观看 | 久久精品国产精品青草 | 狠狠干b| 日本不卡视频网站 | 久久在线看 | 成年女人免费看片 | 国产综合色香蕉精品五月婷 | 国产精品一级毛片不收费 | 狠狠色婷婷狠狠狠亚洲综合 | 国产精品人成在线播放新网站 | 四虎精品视频在线永久免费观看 | 深夜影院深a久久 | 久久久久久夜精品精品免费啦 | 国产成人久久精品一区二区三区 | 中文字幕福利 | 福利视频在线观看午夜 | 99国产视频 |