Android实现自定义圆形进度条
作者:lijiao 发布时间:2022-10-28 04:55:22
今天无意中发现一个圆形进度,想想自己实现一个,如下图:
基本思路是这样的:
1.首先绘制一个实心圆
2.绘制一个白色实心的正方形,遮住实心圆
3.在圆的中心动态绘制当前进度的百分比字符
4.绘制一个与之前实心圆相同颜色的空心圆
5.逐渐改变当前的百分比
6.根据百分比,逐渐改变正方形的大小,逐渐减小正方形的底部y轴的坐标,不断重绘,直到达到100%
首先看看自定义的属性
在values目录下新建attrs.xml内容如下:
定义绘制圆形的背景色,和绘制圆形的半径大小
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="circlecolor" format="color"></attr>
<attr name="half" format="dimension"></attr>
<declare-styleable name="myCircleImage">
<attr name="circlecolor"></attr>
<attr name="half"></attr>
</declare-styleable>
</resources>
自定义视图
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class CirclePro extends View {
private Paint paint;
private int circleBack;//圆的背景色
private int mschedual = 0;//用于控制动态变化
float circleHalf; //圆的半径
String percent = "";//绘制百分比的字符串
@SuppressLint("Recycle")
public CirclePro(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint = new Paint();
TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.myCircleImage, defStyleAttr,0);
@SuppressWarnings("unused")
int leng = array.length();
//获取自定义的属性,这里注意是R.styleable.myCircleImage_circlecolor而不是R.attr.circlecolor
circleBack = array.getColor(R.styleable.myCircleImage_circlecolor,Color.GREEN);
circleHalf = array.getDimension(R.styleable.myCircleImage_half,200.f);
System.out.println(circleBack);
}
/**
* 这个构造参数,当在布局文件中引用该view的时候,必须重写该构造函数
* @param context
* @param attrs
*/
public CirclePro(Context context, AttributeSet attrs) {
this(context, attrs, 0);//调用自己的构造函数
}
/**
* 根据文本的
* @param text
* @param textSize
* @return
*/
public float getTextWidth(String text,float textSize) {
TextPaint textPaint = new TextPaint();
textPaint.setTextSize(textSize);
return textPaint.measureText(text);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
float height = getHeight();
float width = getWidth();
// float circleHalf = (float) (width*0.7/2);
paint.setColor(circleBack);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(width/2,height/2,circleHalf, paint);//画实心圆
if (mschedual <= 100) {//,如果当前进度小于100,画实心矩形
paint.setColor(Color.WHITE);
canvas.drawRect(width/2-circleHalf,height/2-circleHalf,width/2+circleHalf,height/2+circleHalf - mschedual*circleHalf/50, paint);
}
//画当前进度的字符串
paint.setColor(Color.BLACK);
paint.setTextSize(30.f);
percent = mschedual+" %";
canvas.drawText(percent, width/2-getTextWidth(percent,30)/2,height/2+paint.getTextSize()*3/8, paint);//字体的高度=paint.getTextSize()*3/4
//画空心圆
paint.setColor(circleBack);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(width/2,height/2,circleHalf, paint);
if (mschedual < 100) {//更改当前进度值,并重绘
mschedual++;
invalidate();
}
}
}
在activity_main.xml中,需要用到自定义的属性,首先添加命名空间: xmlns:liu=”http://schemas.android.com/apk/res/com.example.androidcirclepro”
其中liu是自定义的一个前缀,随意命名的,com.example.androidcirclepro是我们的应用的包名
activity_main.xmln内容如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:liu="http://schemas.android.com/apk/res/com.example.androidcirclepro" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.example.androidcirclepro.CirclePro android:layout_width="match_parent" android:layout_height="match_parent" liu:half="90dp" liu:circlecolor="#fff0f0" /></RelativeLayout>
至此一个自定义的圆形进度条就完成了,是不是很简单。
猜你喜欢
- 今天可是遇到一个很简单的需求,但是却让我蛋疼了半天。滑动屏幕控制物体旋转,但是旋转的角度要在-60到60之间。乍一听这简直是小儿科啊。判断一
- SpringSecurity基本原理在之前的文章《SpringBoot + Spring Security 基本使用及个性化登录配置》中对S
- 今年新开Java课程第一步就是…配置环境就从Java的环境配置开始好了以下是正式的步骤首先,从Oracle的官网下载jdk的安装包点我下载J
- Spring Boot @RestController重定向redirectSpring MVC项目中页面重定向一般使用return &qu
- 前言:由于最近有解析协议的一些业务场景,需要用到一些字节操作工具,这里封装了一些比较常用的转换方法,测试后基本没有问题,可能一些比较偏门的数
- 在Java编程中经常碰到类型转换,对象类型转换主要包括向上转型和向下转型。向上转型我们在现实中常常这样说:这个人会唱歌。在这里,我们并不关心
- 目录1、对于A、B两种排队方式,说法正确的是2、Inter-process communication (IPC) is the trans
- 如果你发现在一个接口使用有如下定义方法: public String[] getParameters();那么你应该认
- 前言Handler,可谓是面试题中的一个霸主了。在我《面试回忆录》中,几乎没有哪家公司,在面试的时候是不问这个问题的。简单一点,问问使用流程
- 最近因为项目的国际化的需要,需要对整个项目的100来个插件做国际化,这是一件痛苦的事情,因为纯体力劳动。为了省点工作量,想着能不能写个程序批
- Java 线程池原理Executor框架的两级调度模型在HotSpot VM的模型中,Java线程被一对一映射为本地操作系统线程。JAVA线
- 在阿里开发手册的建表规约中有说明,数据库表中应该都要有create_time、update_time字段;那么在开发中,对于这些共有字段的处
- 使用java语言用集合存储数据实现学生信息管理系统,在控制台上编译执行可以实现基本的学生信息增加、删除、修改、查询功能IO版可以参考我的另外
- Java Map.values()方法获取Map集合中的所有键值对象Java 集合类中的 Map.values() 方法用来获取
- 本文实例为大家分享了JAVASE系统实现抽卡功能的具体代码,供大家参考,具体内容如下先看下文件结构使用到的知识点:看下Client类的实现:
- 一般我们用它来自动帮我们注册APT文件(全称是Annotation Process Tool,或者叫注解处理器,AbstractProces
- 一、项目背景在实际工作中,会遇到业务比较集中的情况,随着时间推延,这部分业务关联的mysql表就会越来越大,十分臃肿。尽管在项目架构上做了读
- 1、问题起因最近做项目时遇到了需要多用户之间通信的问题,涉及到了WebSocket握手请求,以及集群中WebSocket Session共享
- cmd调用phantomjs官方资料:http://phantomjs.org/quick-start.html手动执行从官方下载phant
- 一、定义责任链模式(Chain of Responsibility Pattern):避免将一个请求的发送者与接受者耦合在一起,让多个对象都