效果图:
A.绘制圆环,圆弧,文本
B.自定义属性的具体步骤
具体步骤:
1. 定义属性: 在values目录下创建attrs.xml
<declare-styleable name="RoundProgress"> <attr name="roundColor" format="color"></attr> <attr name="roundProgressColor" format="color"></attr> <attr name="textColor" format="color"></attr> <attr name="roundWidth" format="dimension"></attr> <attr name="textSize" format="dimension"></attr></declare-styleable>2. 在布局文件中引用当前应用的名称空间
xmlns:atguigu=http://schemas.android.com/apk/res-auto3. 在自定义视图标签中使用自定义属性
<com.atguigu.p2p.util.RoundProgress android:id="@+id/rp_home_progress" android:layout_width="120dp" android:layout_height="120dp" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" atguigu:roundColor="@android:color/darker_gray <br> atguigu:roundProgressColor="@android:color/holo_red_dark" atguigu:textColor="@color/text_progress" atguigu:roundWidth="10dp" atguigu:textSize="20sp" />4. 在自定义View类的构造方法中, 取出布局中的自定义属性值
//1.得到所有自定义属性的数组TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgress);//2.获取自定义属性的值, 如果没有指定取默认值roundColor = typedArray.getColor(R.styleable.RoundProgress_roundColor, Color.RED);roundProgressColor = typedArray.getColor(R.styleable.RoundProgress_roundProgressColor, Color.GREEN);textColor = typedArray.getColor(R.styleable.RoundProgress_textColor, Color.GREEN);roundWidth = typedArray.getDimension(R.styleable.RoundProgress_roundWidth, UIUtils.dp2px(10));textSize = typedArray.getDimension(R.styleable.RoundProgress_textSize, UIUtils.dp2px(20));//3.释放资源数据typedArray.recycle();C.让圆环进度"动起来"
1.自定义RoundProgress类中提供进度属性的getter和setter方法
2.在HomeFragment的onSuccess()中:
github:https://github.com/ganchuanpu/P2PInvest
以上所述是小编给大家介绍的Android动态自定义圆形进度条,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!