环形ProgressBar的旋转动画效果

发布时间:2024-08-16 16:11:29 作者:小樊
来源:亿速云 阅读:83

要实现环形ProgressBar的旋转动画效果,可以通过自定义View来绘制环形ProgressBar,并使用属性动画来实现旋转效果。

首先,创建一个继承自View的自定义View类,重写onDraw方法来绘制环形ProgressBar:

public class CircularProgressView extends View {

    private Paint paint;
    private RectF rect;
    private int progress = 0;

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

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

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

    private void init() {
        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(20);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);

        rect = new RectF();
    }

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

        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;
        int radius = Math.min(centerX, centerY) - 10;

        rect.set(centerX - radius, centerY - radius, centerX + radius, centerY + radius);

        float angle = 360 * progress / 100;

        canvas.drawArc(rect, -90, angle, false, paint);
    }

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

然后,在Activity或Fragment中使用属性动画来实现旋转效果:

ObjectAnimator animator = ObjectAnimator.ofInt(circularProgressView, "progress", 0, 100);
animator.setDuration(2000);
animator.setInterpolator(new LinearInterpolator());
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.setRepeatMode(ObjectAnimator.RESTART);
animator.start();

这样就可以实现环形ProgressBar的旋转动画效果了。可以根据具体需求调整旋转的速度、方向和颜色等属性。

推荐阅读:
  1. 怎么在Android中利用ProgressBar 模拟进度条
  2. Android ProgressBar怎么实现进度条效果

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

progressbar

上一篇:SeekBar在音频剪辑中的精度控制

下一篇:ProgressBar与RecyclerView的滚动同步

相关阅读

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

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