Android动态自定义圆形进度条
作者:mrr 发布时间:2022-07-21 21:29:50
标签: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()中:
github:https://github.com/ganchuanpu/P2PInvest
以上所述是小编给大家介绍的Android动态自定义圆形进度条网站的支持!
0
投稿
猜你喜欢
- SpringMVC中事务是否可以加在Controller层一般而言,事务都是加在Service层的,但是爱钻牛角尖的我时常想:事务加在Con
- 实例如下:/// <summary> /// 上传ftp服务 /// </summary>
- 本文介绍了Maven+Tomcat8 实现自动化部署的方法,分享给大家,具体如下:1.配置tomcat-users.xml首先在Tomcat
- 本文实例讲述了C#实现将字符串转换成日期格式的方法。分享给大家供大家参考。具体实现方法如下:string s = "2012011
- Kotlin中的一个伟大创前举就是空指针的处理,在代码的编译阶段就能检测可能出现的空指针问题,示例代码如下:data class Perso
- // Create a handler for a click event.button1.Click += delegate(System
- 关于页面渲染其实在工作中,一直都是前后端分离,也就是说,我的工作从来都是提供接口,而不写 html css js 之类的,所以在这方面也没有
- Java持久化框架:Hibernate与MybatisHibernate和Mybatis是两个Java持久化框架,用于将Java对象映射到关
- 前言微信群里的一个提问引发的这个问题,C#异步有多少种实现方式?首先想要知道C#异步有多少中实现方式,首先我们要知道.NET提供的执行异步操
- 1、利用延迟队列延时队列,第一他是个队列,所以具有对列功能第二就是延时,这就是延时对列,功能也就是将任务放在该延时对列中,只有到了延时时刻才
- 背景:新需求需要引入新jar包,引入后发现本地启动没有报错,发到测试环境提示某个bean无法创建,nested exception is j
- 先建个钉钉群,并加好机器人此时,机器人已经添加完毕,接下来编写我们连接机器人小哥的代码import com.alibaba.fastjson
- 一、简介我们在线程编程的时候往往会涉及到线程的通信,通过信号的接受来进行线程是否阻塞的操作。AutoResetEvent 允许线程通过发信号
- 这里记录Java中从控制台读入信息的几种方式,已备后查!(1)JDK 1.4(JDK 1.5和JDK 1.6也都兼容这种方法)public
- spring-AOP 及 AOP获取request各项参数AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事
- 1、什么是委托从数据结构来讲,委托是和类一样是一种用户自定义类型。委托是方法的抽象,它存储的就是一系列具有相同签名和返回回类型的方法的地址。
- JAVA多线程断点下载原理如图:代码如下:import java.io.BufferedReader; import java.io.Fil
- 一. 引入相关jar包首先,我们先在idea里创建出一个Maven项目出来,除了路径要填成自己的希望路径以外,其他都可以一路next和fin
- 介绍Jakarta POI 是一套用于访问微软格式文档的Java API。Jakarta POI有很多组件组成,其中有用于操作Excel格式
- @Transactional 内部调用例子 🌰在 Spring 的 AOP 代理下,只有目标方法由外部调用,目标方法才由 Spring 生成