溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

環(huán)形ProgressBar的旋轉(zhuǎn)動(dòng)畫(huà)效果

發(fā)布時(shí)間:2024-08-16 16:11:29 來(lái)源:億速云 閱讀:83 作者:小樊 欄目:移動(dòng)開(kāi)發(fā)

要實(shí)現(xiàn)環(huán)形ProgressBar的旋轉(zhuǎn)動(dòng)畫(huà)效果,可以通過(guò)自定義View來(lái)繪制環(huán)形ProgressBar,并使用屬性動(dòng)畫(huà)來(lái)實(shí)現(xiàn)旋轉(zhuǎn)效果。

首先,創(chuàng)建一個(gè)繼承自View的自定義View類(lèi),重寫(xiě)onDraw方法來(lái)繪制環(huán)形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中使用屬性動(dòng)畫(huà)來(lái)實(shí)現(xiàn)旋轉(zhuǎn)效果:

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();

這樣就可以實(shí)現(xiàn)環(huán)形ProgressBar的旋轉(zhuǎn)動(dòng)畫(huà)效果了??梢愿鶕?jù)具體需求調(diào)整旋轉(zhuǎn)的速度、方向和顏色等屬性。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI