Android动态自定义圆形进度条
程序员文章站
2023-12-16 14:38:52
效果图:
a.绘制圆环,圆弧,文本
//1.画圆环
//原点坐标
float circlex = width / 2;
float circley...
效果图:
a.绘制圆环,圆弧,文本
//1.画圆环 //原点坐标 float circlex = width / 2; float circley = width / 2; //半径 float radius = width / 2 - roundwidth / 2; //设置画笔的属性 paint.setcolor(roundcolor); paint.setstrokewidth(roundwidth); paint.setstyle(paint.style.stroke); canvas.drawcircle(circlex, circley, radius, paint); //2.画圆弧 rectf oval = new rectf(roundwidth/2,roundwidth/2,width-roundwidth/2,width - roundwidth/2); paint.setcolor(roundprogresscolor); canvas.drawarc(oval, 0, progress * 360 / max, false, paint); //3.画文本 paint.settextsize(textsize); paint.setcolor(textcolor); paint.setstrokewidth(0); string text = progress * 100 / max + "%"; rect bounds = new rect(); paint.gettextbounds(text, 0, text.length(), bounds); canvas.drawtext(text, width / 2 - bounds.width() / 2, width / 2 + bounds.height() / 2, paint);
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-auto
3. 在自定义视图标签中使用自定义属性
<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动态自定义圆形进度条,希望对大家有所帮助