第一步 利用上一个自定义view实现扩展
public class RoundProgressBarWidthNumber extends
HorizontalProgressBarWithNumber
{
/**
* mRadius of view
*/
private int mRadius = dp2px(30);
private int mMaxPaintWidth;
public RoundProgressBarWidthNumber(Context context)
{
this(context, null);
}
public RoundProgressBarWidthNumber(Context context, AttributeSet attrs)
{
super(context, attrs);
mReachedProgressBarHeight = (int) (mUnReachedProgressBarHeight * 2.5f);
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.RoundProgressBarWidthNumber);
mRadius = (int) ta.getDimension(
R.styleable.RoundProgressBarWidthNumber_radius, mRadius);
ta.recycle();
mPaint.setStyle(Style.STROKE);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStrokeCap(Cap.ROUND);
}
/**
* 这里默认在布局中padding值要么不设置,要么全部设置
*/
@Override
protected synchronized void onMeasure(int widthMeasureSpec,
int heightMeasureSpec)
{
mMaxPaintWidth = Math.max(mReachedProgressBarHeight,
mUnReachedProgressBarHeight);
int expect = mRadius * 2 + mMaxPaintWidth + getPaddingLeft()
+ getPaddingRight();
int width = resolveSize(expect, widthMeasureSpec);
int height = resolveSize(expect, heightMeasureSpec);
int realWidth = Math.min(width, height);
mRadius = (realWidth - getPaddingLeft() - getPaddingRight() - mMaxPaintWidth) / 2;
setMeasuredDimension(realWidth, realWidth);
}
@Override
protected synchronized void onDraw(Canvas canvas)
{
String text = getProgress() + "%";
float textWidth = mPaint.measureText(text);
float textHeight = (mPaint.descent() + mPaint.ascent()) / 2;
canvas.save();
canvas.translate(getPaddingLeft() + mMaxPaintWidth / 2, getPaddingTop()
+ mMaxPaintWidth / 2);
mPaint.setStyle(Style.STROKE);
// draw unreaded bar
mPaint.setColor(mUnReachedBarColor);
mPaint.setStrokeWidth(mUnReachedProgressBarHeight);
canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
// draw reached bar
mPaint.setColor(mReachedBarColor);
mPaint.setStrokeWidth(mReachedProgressBarHeight);
float sweepAngle = getProgress() * 1.0f / getMax() * 360;
canvas.drawArc(new RectF(0, 0, mRadius * 2, mRadius * 2), 0,
sweepAngle, false, mPaint);
// draw text
mPaint.setStyle(Style.FILL);
canvas.drawText(text, mRadius - textWidth / 2, mRadius - textHeight,
mPaint);
canvas.restore();
}
}
第二步设置自定义属性
<declare-styleable name="RoundProgressBarWidthNumber">
<attr name="radius" format="dimension" />
</declare-styleable>
<attr name="text" format="string" />
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
第三步 activity_two.xml
<com.imooc.myapplication.view.RoundProgressBarWidthNumber
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dip"
android:background="#44ff0000"
android:padding="15dp"
android:progress="70"
android:id="@+id/id_progress02"
zhy:progress_text_color="#ff2903FC"
zhy:progress_text_size="30sp"
zhy:progress_unreached_color="#ffBCB4E8"
zhy:radius="80dp" />
第四步 TwoActivity.java
public class TwoActivity extends Activity {
private RoundProgressBarWidthNumber mRoundProgressBar;
private static final int MSG_PROGRESS_UPDATE = 0x110;
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
int roundProgress = mRoundProgressBar.getProgress();
mRoundProgressBar.setProgress(++roundProgress);
if (progress >= 100) {
mHandler.removeMessages(MSG_PROGRESS_UPDATE);
}
mHandler.sendEmptyMessageDelayed(MSG_PROGRESS_UPDATE, 100);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
mRoundProgressBar = findViewById(R.id.id_progress02);
mHandler.sendEmptyMessage(MSG_PROGRESS_UPDATE);
}
}