这期内容当中小编将会给大家带来有关Android应用中怎么自定义一个圆形进度条,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
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()中:
上述就是小编为大家分享的Android应用中怎么自定义一个圆形进度条了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。