溫馨提示×

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

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

Android項(xiàng)目中使用Progress實(shí)現(xiàn)一個(gè)環(huán)形進(jìn)度條

發(fā)布時(shí)間:2020-11-23 17:26:26 來(lái)源:億速云 閱讀:192 作者:Leah 欄目:移動(dòng)開(kāi)發(fā)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Android項(xiàng)目中使用Progress實(shí)現(xiàn)一個(gè)環(huán)形進(jìn)度條,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

效果圖:

Android項(xiàng)目中使用Progress實(shí)現(xiàn)一個(gè)環(huán)形進(jìn)度條

代碼實(shí)現(xiàn):

設(shè)置已完成步數(shù)和目標(biāo)步數(shù):

  public void setStep(int stepDone, int stepGoal) {
    this.stepDone = stepDone;
    this.stepGoal = stepGoal;
    int progess = (stepDone * 100) / stepGoal;
    if (progess > 100) {
      setProgress(100);
    } else {
      setProgress(progess);
    }
  }

設(shè)置進(jìn)度:

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

設(shè)置畫(huà)筆屬性:

mPaint.setAntiAlias(true);
mPaint.setColor(Color.rgb(0xe9, 0xe9, 0xe9));
canvas.drawColor(Color.TRANSPARENT);
mPaint.setStrokeWidth(LINE_WIDTH_BG);
mPaint.setStyle(Paint.Style.STROKE);

繪制環(huán)形和背景:

canvas.drawArc(mRectF, -90, 360, false, mPaint);
mPaint.setColor(Color.rgb(0xf8, 0x60, 0x30));
canvas.drawArc(mRectF, -90, ((float) mProgress / mMaxProgress) * 360, false, mPaint);

繪制步數(shù)和單位:

mPaint.setStrokeWidth(TEXT_WIDTH);
    String text = stepDone + context.getString(R.string.step_unit);
    int textHeight = height / 4;
    mPaint.setTextSize(textHeight);
    int textWidth = (int) mPaint.measureText(text, 0, text.length());
    mPaint.setStyle(Paint.Style.FILL);
    canvas.drawText(text, width / 2 - textWidth / 2, height / 2 + textHeight / 4, mPaint);

繪制目標(biāo)步數(shù):

 String textGoal = "/" + stepGoal;
    int textGoalHeight = height / 8;
    mPaint.setTextSize(textGoalHeight);
    int textGoalWidth = (int) mPaint.measureText(textGoal, 0, textGoal.length());
    mPaint.setStyle(Paint.Style.FILL);
    canvas.drawText(textGoal, width / 2 - textGoalWidth / 2, height / 2 + textHeight / 2
        + textGoalHeight, mPaint);

上述就是小編為大家分享的Android項(xiàng)目中使用Progress實(shí)現(xiàn)一個(gè)環(huán)形進(jìn)度條了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向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