要实现弯曲的进度条动画,可以通过自定义 View 来实现。具体实现步骤如下:

  1. 创建自定义 View 类

在项目中创建一个自定义 View 类,可以继承自 View 或者自定义 ViewGroup。

  1. 重写 onDraw 方法

在 onDraw 方法中绘制进度条的背景和进度条的弯曲部分。

  1. 添加动画效果

使用 ValueAnimator 或者 ObjectAnimator 添加动画效果,不断改变进度条的进度值,从而实现进度条的动画效果。

  1. 设置属性和方法

为自定义 View 类添加属性和方法,如设置进度条的颜色、宽度、进度值等。

以下是一个简单的示例代码:

public class CurvedProgressBar extends View {

    private int mProgress = 0;
    private int mMax = 100;
    private int mBackgroundColor = Color.GRAY;
    private int mProgressColor = Color.BLUE;
    private float mStrokeWidth = 10;
    private RectF mRectF;
    private Paint mBackgroundPaint;
    private Paint mProgressPaint;

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

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

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

    private void init() {
        mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBackgroundPaint.setColor(mBackgroundColor);
        mBackgroundPaint.setStyle(Paint.Style.STROKE);
        mBackgroundPaint.setStrokeWidth(mStrokeWidth);

        mProgressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mProgressPaint.setColor(mProgressColor);
        mProgressPaint.setStyle(Paint.Style.STROKE);
        mProgressPaint.setStrokeWidth(mStrokeWidth);

        mRectF = new RectF();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float sweepAngle = (float) mProgress / mMax * 360;
        canvas.drawArc(mRectF, 135, 270, false, mBackgroundPaint);
        canvas.drawArc(mRectF, 135, sweepAngle, false, mProgressPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
        int centerX = getMeasuredWidth() / 2;
        int centerY = getMeasuredHeight() / 2;
        int radius = size / 2 - (int) mStrokeWidth / 2;
        mRectF.set(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
    }

    public void setProgress(int progress) {
        mProgress = progress;
        invalidate();
    }

    public void setMax(int max) {
        mMax = max;
    }

    public void setBackgroundColor(int backgroundColor) {
        mBackgroundColor = backgroundColor;
        mBackgroundPaint.setColor(mBackgroundColor);
    }

    public void setProgressColor(int progressColor) {
        mProgressColor = progressColor;
        mProgressPaint.setColor(mProgressColor);
    }

    public void setStrokeWidth(float strokeWidth) {
        mStrokeWidth = strokeWidth;
        mBackgroundPaint.setStrokeWidth(mStrokeWidth);
        mProgressPaint.setStrokeWidth(mStrokeWidth);
        requestLayout();
    }

    public void startAnimation() {
        ValueAnimator animator = ValueAnimator.ofInt(0, mMax);
        animator.setDuration(3000);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int progress = (int) animation.getAnimatedValue();
                setProgress(progress);
            }
        });
        animator.start();
    }
}

使用时,可以在布局文件中添加以下代码:

<com.example.myapplication.CurvedProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="200dp"
    android:layout_height="200dp"
    app:backgroundColor="@android:color/darker_gray"
    app:progressColor="@android:color/holo_blue_bright"
    app:strokeWidth="20dp" />

然后在代码中调用 startAnimation 方法即可开始动画。

Android 自定义弯曲进度条动画实现教程

原文地址: https://www.cveoy.top/t/topic/ozkJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录