环形ProgressBar动画效果的自定义

发布时间:2024-08-16 15:31:30 作者:小樊
来源:亿速云 阅读:81

要实现环形ProgressBar的动画效果,可以借助ValueAnimator和Canvas来实现。以下是一个简单的示例代码:

public class CircleProgressBar extends View {

    private Paint mPaint;
    private int mProgress;
    private int mMaxProgress;
    private int mBackgroundColor;
    private int mProgressColor;
    private ValueAnimator mAnimator;

    public CircleProgressBar(Context context) {
        this(context, null);
    }

    public CircleProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);

        mProgress = 0;
        mMaxProgress = 100;
        mBackgroundColor = Color.GRAY;
        mProgressColor = Color.BLUE;

        mAnimator = ValueAnimator.ofInt(0, mMaxProgress);
        mAnimator.setDuration(1000);
        mAnimator.addUpdateListener(animation -> {
            mProgress = (int) animation.getAnimatedValue();
            invalidate();
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = getWidth();
        int height = getHeight();

        float centerX = width / 2;
        float centerY = height / 2;
        float radius = Math.min(width, height) / 2 - 10;

        // 绘制背景圆环
        mPaint.setColor(mBackgroundColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(20);
        canvas.drawCircle(centerX, centerY, radius, mPaint);

        // 绘制进度圆环
        mPaint.setColor(mProgressColor);
        float sweepAngle = 360f * mProgress / mMaxProgress;
        canvas.drawArc(centerX - radius, centerY - radius, centerX + radius, centerY + radius, -90, sweepAngle, false, mPaint);
    }

    public void setProgress(int progress) {
        if (progress >= 0 && progress <= mMaxProgress) {
            mProgress = progress;
            invalidate();
        }
    }

    public void startAnimation() {
        mAnimator.start();
    }

    public void setMaxProgress(int maxProgress) {
        mMaxProgress = maxProgress;
    }

    public void setBackgroundColor(int color) {
        mBackgroundColor = color;
    }

    public void setProgressColor(int color) {
        mProgressColor = color;
    }
}

在布局文件中添加CircleProgressBar,并设置属性:

<com.example.CircleProgressBar
    android:id="@+id/circleProgressBar"
    android:layout_width="100dp"
    android:layout_height="100dp"
    app:backgroundColor="@color/gray"
    app:progressColor="@color/blue" />

在代码中可以通过调用setProgress方法更新进度,通过调用startAnimation方法启动动画效果。可以根据需要调整画笔样式、动画时长等属性来实现不同的环形ProgressBar动画效果。

推荐阅读:
  1. Android如何实现ProgressBar进度条和ProgressDialog进度框
  2. Android中如何使用progressbar实现带底部指示器和文字进度条

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

progressbar

上一篇:SeekBar在滑动列表中的性能优化

下一篇:如何让SeekBar响应更灵敏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》