圖片循環滾動控件ScrollingImageView

設置一個圖片,控件可以自動循環滾動,單張圖片首尾拼接

/**
 * 可滾動的ImageView控件,使用startRolling(int) 或者 startRolling(Bitmap)啓動滾動
 * @author brian512
 */
public class ScrollingImageView extends View {

    public final float SPEED_SLOW = 1f;
    public final float SPEED_NORMAL = 3f;
    public final float SPEED_FAST = 10f;

    private Bitmap mBitmap;

    private int mResourceID = 0;

    private float mSpeed = SPEED_NORMAL;

    private Rect clipBounds = new Rect(); // 用於保存邊界信息

    private float offset = 0; // 記錄起始位置,需要拼接

    private boolean mIsScrolling; // 標記是否開始滾動

    public ScrollingImageView(Context context) {
        this(context, null, 0);
    }

    public ScrollingImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScrollingImageView(Context context, AttributeSet attrs, int styleID) {
        super(context, attrs, styleID);
        //startScroll(R.drawable.loading_bg);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), heightMeasureSpec);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (canvas == null) {
            return;
        }
        Bitmap bitmap = getBitmap();
        if (bitmap == null || bitmap.isRecycled()) {
            return;
        }
        canvas.getClipBounds(clipBounds);


        int width = bitmap.getWidth();
        while (offset <= -width) {
            offset += width;
        }
        float left = offset;

        while (left < clipBounds.width()) {
            canvas.drawBitmap(bitmap, getBitmapLeft(width, left), 0, null);
            left += width;
        }

        if (mIsScrolling && mSpeed != 0) {
            offset -= Math.abs(mSpeed);
            postInvalidateOnAnimation();
        }
    }

    private Bitmap getBitmap() {
        if (mBitmap == null) {
            mBitmap = BitmapFactory.decodeResource(getResources(), mResourceID);
        }

        return mBitmap;
    }

    private float getBitmapLeft(float layerWidth, float left) {
        if (mSpeed < 0) {
            return clipBounds.width() - layerWidth - left;
        } else {
            return left;
        }
    }

    /**
     * 開始滾動
     */
    public void startScroll(int imageID) {
        if (imageID <= 0) {
            return;
        }
        mResourceID = imageID;
        start();
    }

    /**
     * 開始滾動
     */
    public void startScroll(Bitmap bitmap) {
        if (bitmap == null || bitmap.isRecycled()) {
            return;
        }
        mBitmap = bitmap;
        start();
    }

    private void start() {
        if (!mIsScrolling) {
            mIsScrolling = true;
            postInvalidateOnAnimation();
        }
    }

    /**
     * 停止滾動
     */
    public void stop() {
        if (mIsScrolling) {
            mIsScrolling = false;
            invalidate();
        }
    }

    /**
     * 設置滾動速度
     * @param speed 速度,默認爲 SPEED_NORMAL = 3f
     */
    public void setSpeed(float speed) {
        this.mSpeed = speed;
        if (mIsScrolling) {
            postInvalidateOnAnimation();
        }
    }

    /**
     * 獲取滾動速度
     * @return
     */
    public float getSpeed() {
        return mSpeed;
    }

    /**
     * 是否在滾動
     * @return
     */
    public boolean getIsScrolling() {
        return mIsScrolling;
    }

}

參考github開源項目:
https://github.com/Q42/AndroidScrollingImageView

發佈了124 篇原創文章 · 獲贊 114 · 訪問量 226萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章