Android动态自定义圆形进度条

Android动态自定义圆形进度条,第1张

概述效果图:A.绘制圆环,圆弧,文本//1.画圆环//原点坐标floatcircleX=width/2;floatcircleY=width/2;

效果图:

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,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,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动态自定义圆形进度条,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android动态自定义圆形进度条全部内容,希望文章能够帮你解决Android动态自定义圆形进度条所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1146883.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存