自定義View系列(六) 進度條實現

自定義進度條,其實實現并不難,和運動步數的實現差不多。

1.自定義屬性先上
<declare-styleable name="ProgressBar">
    <attr name="innerBackground" format="color"/>
    <attr name="outerBackground" format="color"/>
    <attr name="roundWidth" format="dimension"/>
    <attr name="progressTextSize" format="dimension"/>
    <attr name="progressTextColor" format="color"/>
</declare-styleable>

public class ProgressBar extends View {

private int mInnerBackground = Color.RED;
private int mOuterBackground = Color.RED;
private int mRoundWidth = 10;// 10px
private float mProgressTextSize = 15;
private int mProgressTextColor = Color.RED;

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

public ProgressBar(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    // 獲取自定義屬性
    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ProgressBar);
    mInnerBackground = array.getColor(R.styleable.ProgressBar_innerBackground, mInnerBackground);
    mOuterBackground = array.getColor(R.styleable.ProgressBar_outerBackground, mOuterBackground);
    mRoundWidth = (int) array.getDimension(R.styleable.ProgressBar_roundWidth, dip2px(10));
    mProgressTextSize = array.getDimensionPixelSize(R.styleable.ProgressBar_progressTextSize,
            sp2px(mProgressTextSize));
    mProgressTextColor = array.getColor(R.styleable.ProgressBar_progressTextColor, mProgressTextColor);

    array.recycle();
}
2.畫筆緊跟其后
  private Paint mInnerPaint, mOuterPaint, mTextPaint;

    mInnerPaint = new Paint();
    mInnerPaint.setAntiAlias(true);
    mInnerPaint.setColor(mInnerBackground);
    mInnerPaint.setStrokeWidth(mRoundWidth);
    mInnerPaint.setStyle(Paint.Style.STROKE);

    mOuterPaint = new Paint();
    mOuterPaint.setAntiAlias(true);
    mOuterPaint.setColor(mOuterBackground);
    mOuterPaint.setStrokeWidth(mRoundWidth);
    mOuterPaint.setStyle(Paint.Style.STROKE);

    mTextPaint = new Paint();
    mTextPaint.setAntiAlias(true);
    mTextPaint.setColor(mProgressTextColor);
    mTextPaint.setTextSize(mProgressTextSize);
3.測量
   @Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    // 只保證是正方形
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    setMeasuredDimension(Math.min(width, height), Math.min(width, height));
}
4. 先畫內圓
    int center = getWidth() / 2;
    canvas.drawCircle(center, center, center - mRoundWidth / 2, mInnerPaint);
5.再畫外圓
    RectF rect = new RectF(0 + mRoundWidth / 2, 0 + mRoundWidth / 2,
            getWidth() - mRoundWidth / 2, getHeight() - mRoundWidth / 2);

    if (mProgress == 0) {
        return;
    }
    float percent = (float) mProgress / mMax;
    canvas.drawArc(rect, 0, percent * 360, false, mOuterPaint);
6.畫文字
    String text = ((int) (percent * 100)) + "%";
    Rect textBounds = new Rect();
    mTextPaint.getTextBounds(text, 0, text.length(), textBounds);
    int x = getWidth() / 2 - textBounds.width() / 2;

    Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt();
    int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
    int baseLineY = getHeight() / 2 + dy;
    canvas.drawText(text, x, baseLineY, mTextPaint);
7.提供方法
public synchronized void setMax(int max) {
    if (max < 0) {

    }
    this.mMax = max;
}

public synchronized void setProgress(int progress) {
    if (progress < 0) {
    }
    this.mProgress = progress;
    // 刷新 invalidate
    invalidate();
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容