单张图片逐步显示的动画

2022-05-18  本文已影响0人  MacLi

单张图片逐步显示的动画,参考代码

package com.desaysv.vehiclesetting.window.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.desaysv.vehiclesetting.R;

/**
 * 充电进度视图
 */
public class ProgressView extends View {

    private Paint mPaint;

    private int mWidth = 0;
    private int mHeight = 0;

    /**
     * 进度 Path
     */
    private final Path mProPath = new Path();

    /**
     * 当前进度
     */
    private int mProgress = 0;

    /**
     * 总进度
     */
    private final int maxProgress = 100;

    private Bitmap mSourceBitmap;

    private int mProWidth;

    private PorterDuffXfermode mXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);

    private Canvas mCanvas;

    private Rect mSrcRect;
    private Rect mDstRect;

    private Bitmap mFinalBitmap;

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

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

    public void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);

//        PathEffect pathEffect = new CornerPathEffect(getResources().getDimensionPixelOffset(R.dimen.dp_5));
//        mPaint.setPathEffect(pathEffect);

        mSourceBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.window_open_00000);
//        mSourceBitmap = DrawableToBitmap(getBackground());
//        Log.e("ProgressView", "mSourceBitmap.getWidth()=" + mSourceBitmap.getWidth() + ",mSourceBitmap.getHeight()=" + mSourceBitmap.getHeight());
//        mSrcRect = new Rect(0, 0, mSourceBitmap.getWidth(), mSourceBitmap.getHeight());
//        mDstRect = new Rect(0, 0, mWidth, mHeight);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        if (width != mWidth && height != mHeight) {
            mWidth = width;
            mHeight = height;
            mDstRect = new Rect(0, 0, mWidth, mHeight);
//            Log.e("ProgressView", "mWidth=" + mWidth + ",mHeight=" + mHeight);
            mSourceBitmap = BitmapScale(mSourceBitmap, mWidth, mHeight);
//            Log.e("ProgressView", "2.mSourceBitmap.getWidth()=" + mSourceBitmap.getWidth() + ",mSourceBitmap.getHeight()=" + mSourceBitmap.getHeight());
            mSrcRect = new Rect(0, 0, mSourceBitmap.getWidth(), mSourceBitmap.getHeight());
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mSrcRect.top = mHeight - mProgress;
        mSrcRect.left = 0;
        mSrcRect.right = mSourceBitmap.getWidth();
        mSrcRect.bottom = mHeight;

        mDstRect.top = mHeight - mProgress;
        mDstRect.left = 0;
        mDstRect.right = mSourceBitmap.getWidth();
        mDstRect.bottom = mHeight;
//        Log.e("ProgressView", "mSrcRect=" + mSrcRect.toString() + "," + mProgress);
//        Log.e("ProgressView", "mDstRect=" + mDstRect.toString() + "," + mProgress);
        canvas.drawBitmap(mSourceBitmap, mSrcRect, mDstRect, mPaint);
    }

    /**
     * 设置进度
     */
    public void setProgress(int progress) {
//        Log.e("ProgressView", "progress=" + progress);
        if (progress == mProgress) {
            return;
        }
        if (progress > mHeight) {
            setVisibility(View.INVISIBLE);
            return;
        }
        mProgress = progress;
        invalidate();
    }

//    public void setResourceId(int resourceId) {
//        mSourceBitmap = BitmapFactory.decodeResource(getResources(), resourceId);
//    }

    public void startAnim() {
        setVisibility(View.VISIBLE);
        new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (i++ <= mHeight) {
                    setProgress(i);
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    // 5. Drawable----> Bitmap
    public static Bitmap DrawableToBitmap(Drawable drawable) {
        // 获取 drawable 长宽
        int width = drawable.getIntrinsicWidth();
        int heigh = drawable.getIntrinsicHeight();
        drawable.setBounds(0, 0, width, heigh);
        // 获取drawable的颜色格式
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        // 创建bitmap
        Bitmap bitmap = Bitmap.createBitmap(width, heigh, config);
        // 创建bitmap画布
        Canvas canvas = new Canvas(bitmap);
        // 将drawable 内容画到画布中
        drawable.draw(canvas);
        return bitmap;
    }

    public static Bitmap BitmapScale(Bitmap srcBitmap, int newWidth, int newHeight) {
        int width = srcBitmap.getWidth();
        int height = srcBitmap.getHeight();
        //计算压缩的比率
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        return Bitmap.createBitmap(srcBitmap, 0, 0, width, height, matrix, true);
    }

}

上一篇下一篇

猜你喜欢

热点阅读