Android自定义View-一张背景图片移动

2020-04-30  本文已影响0人  LuckySuperMan

产品需求:

一张背景图从右到左缓慢移动,无限循环!


login_bg2.jpg

上效果图:

QQ20200430-102715-HD.gif

思路:

1.先把图片读取出来修改尺寸(以屏幕高度为标准缩放图片大小)。
2.设置属性动画ValueAnimator.ofInt(0, mBitmapW) //mBitmapW是图片修改过后宽度。
3.通过mValue裁减图片进行绘制。

然后直接上代码:

public class RollImageView extends View {
    private int mBitmapH;
    private int mBitmapW;
    private Bitmap mBitmap;
    private int mW;
    private int mH;
    private ValueAnimator mValueAnimator;
    private int mValue;

    private Rect mSrcRect = new Rect();
    private Rect mDestRect = new Rect();

    public RollImageView(Context context) {
        super(context);
        init();
    }

    public RollImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RollImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mW = w;
        mH = h;
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.login_bg2);
        mBitmapH = mBitmap.getHeight();
        mBitmapW = mBitmap.getWidth();
        int scaleWidth = (int) (((float)  mH / (float) mBitmapH) * mBitmapW);
        int scaleHeight = mH;
        mBitmap = Bitmap.createScaledBitmap(mBitmap, scaleWidth, scaleHeight, true);
        mBitmapH = mBitmap.getHeight();
        mBitmapW = mBitmap.getWidth();
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        mValueAnimator = ValueAnimator.ofInt(0, mBitmapW);
        mValueAnimator.setDuration(mBitmapW*5);
        mValueAnimator.setRepeatCount(-1);
        mValueAnimator.setInterpolator(new LinearInterpolator());
        mValueAnimator.addUpdateListener(valueAnimator -> {
            mValue = (int) valueAnimator.getAnimatedValue();
            invalidate();
        });

        mValueAnimator.start();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mSrcRect.set(mValue, 0, mW + mValue, mBitmapH);
        mDestRect.set(0, 0, mW, mH);
        canvas.drawBitmap(mBitmap, mSrcRect, mDestRect, null);

        if (mValue >= mBitmapW - mW) {
            mSrcRect.set(0, 0, mValue - (mBitmapW - mW), mBitmapH);
            mDestRect.set(mW - (int) ((float) (mValue - (mBitmapW - mW)) / ((float) mBitmapH / (float) mH)), 0, mW, mH);
            canvas.drawBitmap(mBitmap, mSrcRect, mDestRect, null);
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = measureWidth(widthMeasureSpec);
        int height = measureHeight(heightMeasureSpec);
        //设置宽高
        setMeasuredDimension(width, height);
    }

    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //wrap_content
        if (specMode == MeasureSpec.AT_MOST) {
        }
        //fill_parent或者精确值
        else if (specMode == MeasureSpec.EXACTLY) {

        }
        return specSize;
    }

    //根据xml的设定获取高度
    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //wrap_content
        if (specMode == MeasureSpec.AT_MOST) {
//            specSize = mBitmapH;
        }
        //fill_parent或者精确值
        else if (specMode == MeasureSpec.EXACTLY) {
        }
        return specSize;
    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mValueAnimator != null && mValueAnimator.isRunning()) {
            mValueAnimator.cancel();
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读