自定义view之炫酷的进度条(二)

第一步 利用上一个自定义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);

}

}

效果图

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值