自定义CircleProgressView

背景

在网上看到一个效果,虽然很简单,但是做的很漂亮我很喜欢,正好在学习自定义View,于是拿来练练手。来看下效果,直接用的人家的效果图,后面给出连接,不过我做的是个简化版的,毕竟是拿来练手的。

分析View中的元素

  • 背景圆
  • 进度条弧线
  • 进度条头部的圆
  • 中间的进度文字

实现

  • 画一个空心的背景圆,需要圆心坐标、半径和宽度,这个很简单;
  • 绘制进度,需要计算出弧的圆心角度数、起始点、宽度(不能小于背景圆的宽度吧);
  • 进度头的圆是2个,内圆和外圆,圆心位置是关键,需要用到三角函数,也很简单;
  • 中间的文字绘制关键点是确定文字的起始位置;

确定需要的属性

新建res/values/attr.xml,定义如下属性

<declare-styleable name="CircleProgressView">
    <attr name="background_circle_width" format="dimension" />
    <attr name="background_circle_color" format="color" />

    <attr name="progress_width" format="dimension" />
    <attr name="progress_color" format="color" />

    <attr name="progress_percent" format="integer" />

    <attr name="progress_text_size" format="dimension" />
    <attr name="progress_text_color" format="color" />

    <attr name="progress_circle_width" format="dimension"/>
</declare-styleable>

在构造方法中获取定义的属性

private void initAttrs(Context context, AttributeSet attributeSet) {
    TypedArray ta = context.obtainStyledAttributes(attributeSet, R.styleable.CircleProgressView);
    mBackgroundCircleWidth = ta.getDimension(R.styleable.CircleProg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值