簡易進(jìn)度條

話不多少,上代碼:

public class MyProgressView extends View {

private Paint mPant;
private Paint textPant;
private String color = "#3F51B5";

private RectF rectF;
private int nowAngle = 0;

private Path path;

//動畫是否完成
private boolean circleFinished = false;


public MyProgressView(Context context) {
    super(context, null);
    intView();
}

public MyProgressView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs, 0);
    intView();
}

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

private void intView() {
    textPant = new Paint();
    textPant.setColor(Color.parseColor(color));
    textPant.setStrokeWidth(1);
    textPant.setStyle(Paint.Style.FILL);
    textPant.setTextAlign(Paint.Align.CENTER);
    
    mPant = new Paint();
    mPant.setColor(Color.parseColor(color));
    mPant.setStrokeWidth(10);
    mPant.setStyle(Paint.Style.STROKE);

    path = new Path();

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            int min=(getHeight() > getWidth() ? getWidth() : getHeight())/2;

            int startX = (min / 2 / 2);
            int startY = (min / 2 / 2);
            path.moveTo(startX, startY);
            for (int i = 0; i < (min / 2 / 2); i++) {
                path.lineTo(startX + 1, startY + 1);
                startX = startX + 1;
                startY = startY + 1;
            }

            for (int i = 0; i < (min / 2); i++) {
                path.lineTo(startX + i + 5, startY - i);
            }
        }
    }, 400);
}


/**
 * 設(shè)置進(jìn)度
 */
public void setPencent(int pencent) {
    nowAngle = (pencent * 360 / 100);
    circleFinished = false;
    invalidate();

    if (nowAngle == 360) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                int min=(getHeight() > getWidth() ? getWidth() : getHeight())/2;
                ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f, min / 2, min / 2);
                scaleAnimation.setDuration(300);
                MyProgressView.this.startAnimation(scaleAnimation);
                circleFinished = true;
            }
        }, 200);
    }
}

/**
 * 設(shè)置顏色
 *
 * @param color
 */
public void setColor(String color) {
    this.color = color;
    mPant.setColor(Color.parseColor(color));
    textPant.setColor(Color.parseColor(color));
    invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    
    int min=(getHeight() > getWidth() ? getWidth() : getHeight())/2;
    //畫圓環(huán)
    rectF = new RectF(10,10,min ,min);
    canvas.drawArc(rectF, -90, nowAngle, false, mPant);


    //畫文本
    if (nowAngle == 0) {
        textPant.setTextSize(min / 8);
        canvas.drawText("點(diǎn)擊下載", min/2, min/2, textPant);
    }else if(!circleFinished){
        textPant.setTextSize(min / 8);
        canvas.drawText(((nowAngle * 100) / 360) + "%", min / 2, min / 2, textPant);
    }
    

    //畫√
    if (circleFinished) {
        canvas.drawPath(path, mPant);
    }
}

}

調(diào)用setPencent直接設(shè)置進(jìn)度,完成后有個(gè)縮放動畫

image.png
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,094評論 25 709
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,351評論 0 17
  • 快樂幸福的童年 我興致勃勃的幫助媽媽干活了,我先去廚房拿了塊抹布,擦起了茶機(jī),左擦擦右擦擦,擦完了茶幾我又去拿笤帚...
    知足常樂朵兒閱讀 794評論 0 3
  • 回到家,父親就給我安排了個(gè)活,幫他朋友看看項(xiàng)目。我心想,老家大都傳統(tǒng)生意,重度依賴關(guān)系人脈,我能做什么呢? 這人確...
    心甲閱讀 187評論 1 0
  • 她想她不懂這棟樓的構(gòu)造, 人們都說 她年紀(jì)太小 一把火就可以縱情燃燒 ---唐八妹
    關(guān)馨仁閱讀 254評論 0 1

友情鏈接更多精彩內(nèi)容