github:https://github.com/HarryXR/android
- 水平進(jìn)度條
測量
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int textHeight = ViewUtils.getTextHeight(mAlertTextPaint);
int height = 0;
if (!TextUtils.isEmpty(mLeftAlert) || !TextUtils.isEmpty(mRightAlert)) {
height += textHeight; //加上文字高度
}
height += LINE_MARGIN;
height += STROKE_WIDTH * 2;
height += LINE_MARGIN;
if (!TextUtils.isEmpty(mLeftContent) || !TextUtils.isEmpty(mRightContent)) {
height += textHeight;
}
setMeasuredDimension(widthMeasureSpec, height);}
ViewUtils測量文本高度
public static int getTextHeight(Paint paint) {
Paint.FontMetrics fontMetrics = paint.getFontMetrics();
return (int) Math.ceil(fontMetrics.descent - fontMetrics.ascent);}
畫進(jìn)度
private void drawProgress(Canvas canvas) {
//畫灰色進(jìn)度
Paint bgPaint = getProgressPaint();
bgPaint.setColor(mProgressBg);
//左右扣除半個圓距離
canvas.drawLine(STROKE_WIDTH / 2, mHeight / 2, mWidth - STROKE_WIDTH / 2, mHeight / 2, bgPaint);
//畫綠色進(jìn)度
Paint progressPaint = getProgressPaint(); progressPaint.setColor(mProgressColor);
int stopX = (int) ((mWidth - STROKE_WIDTH) * mProgress);
canvas.drawLine(STROKE_WIDTH / 2, mHeight / 2, stopX, mHeight / 2, progressPaint);
//畫指示
Paint textPaint = getIndicatorPaint();
int textWidth = ViewUtils.getTextWidth(textPaint, mAlert);
Paint indicatorPaint = getProgressPaint();
indicatorPaint.setColor(mIndicatorBg);
indicatorPaint.setStrokeWidth(ALERT_STROKE_WIDTH);
canvas.drawLine(stopX - textWidth / 2, mHeight / 2, stopX + textWidth / 2, mHeight / 2, indicatorPaint);
//畫指示文字
Paint.FontMetrics fm = textPaint.getFontMetrics();
int alertY = mHeight / 2 + (int) (Math.abs(fm.bottom + fm.top)) / 2;
canvas.drawText(mAlert, stopX - textWidth / 2, alertY, textPaint);}
- 圓環(huán)
大家看源碼吧,涉及到一些數(shù)學(xué)公式的計算,原理類似