android自定义进度条渐变圆形
作者:mrr 发布时间:2022-07-15 02:04:14
标签:android,进度条
在安全卫生上,经常看到有圆形的进度条在转动,效果非常好看,于是就尝试去实现一下,具体实现过程不多说了,直接上效果图,先炫耀下。
效果图:
分析:比较常见于扫描结果、进度条等场景
利用canvas.drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)绘制圆弧
Paint的一些属性定义粗细、颜色、样式等
LinearGradient实现颜色的线型渐变
同样的道理,可以画出长条进度条,扇图饼图等,感兴趣可以试下..
package com.liujing.progressviewdemo;
/***
* 自定义圆弧进度条
*
* @author liujing
*/
public class ProgressView extends View {
//分段颜色
private static final int[] SECTION_COLORS = { Color.GREEN, Color.YELLOW,
Color.RED };
private static final String[] ALARM_LEVEL = { "安全", "低危", "中危", "高危" };
private float maxCount;
private float currentCount;
private int score;
private String crrentLevel;
private Paint mPaint;
private Paint mTextPaint;
private int mWidth, mHeight;
public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
public ProgressView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ProgressView(Context context) {
this(context, null);
}
private void init(Context context) {
mPaint = new Paint();
mTextPaint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
initPaint();
RectF rectBlackBg = new RectF(20, 20, mWidth - 20, mHeight - 20);
canvas.drawArc(rectBlackBg, 0, 360, false, mPaint);
mPaint.setColor(Color.BLACK);
canvas.drawText(score + "分", mWidth / 2, mHeight / 2, mTextPaint);
mTextPaint.setTextSize(40);
if (crrentLevel != null) {
canvas.drawText(crrentLevel, mWidth / 2, mHeight / 2 + 50,
mTextPaint);
}
float section = currentCount / maxCount;
if (section <= 1.0f / 3.0f) {
if (section != 0.0f) {
mPaint.setColor(SECTION_COLORS[0]);
} else {
mPaint.setColor(Color.TRANSPARENT);
}
} else {
int count = (section <= 1.0f / 3.0f * 2) ? 2 : 3;
int[] colors = new int[count];
System.arraycopy(SECTION_COLORS, 0, colors, 0, count);
float[] positions = new float[count];
if (count == 2) {
positions[0] = 0.0f;
positions[1] = 1.0f - positions[0];
} else {
positions[0] = 0.0f;
positions[1] = (maxCount / 3) / currentCount;
positions[2] = 1.0f - positions[0] * 2;
}
positions[positions.length - 1] = 1.0f;
LinearGradient shader = new LinearGradient(3, 3, (mWidth - 3)
* section, mHeight - 3, colors, null,
Shader.TileMode.MIRROR);
mPaint.setShader(shader);
}
canvas.drawArc(rectBlackBg, 180, section * 360, false, mPaint);
}
private void initPaint() {
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth((float) 40.0);
mPaint.setStyle(Style.STROKE);
mPaint.setStrokeCap(Cap.ROUND);
mPaint.setColor(Color.TRANSPARENT);
mTextPaint.setAntiAlias(true);
mTextPaint.setStrokeWidth((float) 3.0);
mTextPaint.setTextAlign(Paint.Align.CENTER);
mTextPaint.setTextSize(50);
mTextPaint.setColor(Color.BLACK);
}
private int dipToPx(int dip) {
float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
}
public int getScore() {
return score;
}
public String getCrrentLevel() {
return crrentLevel;
}
public void setCrrentLevel(String crrentLevel) {
this.crrentLevel = crrentLevel;
}
public float getMaxCount() {
return maxCount;
}
public float getCurrentCount() {
return currentCount;
}
public void setScore(int score) {
this.score = score;
if (score == 100) {
this.crrentLevel = ALARM_LEVEL[0];
} else if (score >= 70 && score < 100) {
this.crrentLevel = ALARM_LEVEL[1];
} else if (score >= 30 && score < 70) {
this.crrentLevel = ALARM_LEVEL[2];
} else {
this.crrentLevel = ALARM_LEVEL[3];
}
invalidate();
}
/***
* 设置最大的进度值
*
* @param maxCount
*/
public void setMaxCount(float maxCount) {
this.maxCount = maxCount;
}
/***
* 设置当前的进度值
*
* @param currentCount
*/
public void setCurrentCount(float currentCount) {
this.currentCount = currentCount > maxCount ? maxCount : currentCount;
invalidate();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthSpecMode == MeasureSpec.EXACTLY
|| widthSpecMode == MeasureSpec.AT_MOST) {
mWidth = widthSpecSize;
} else {
mWidth = 0;
}
if (heightSpecMode == MeasureSpec.AT_MOST
|| heightSpecMode == MeasureSpec.UNSPECIFIED) {
mHeight = dipToPx(15);
} else {
mHeight = heightSpecSize;
}
setMeasuredDimension(mWidth, mHeight);
}
}
Demo:http://xiazai.jb51.net/201507/yuanma/ProgressViewDemo(jb51.net).rar
以上内容就是实现android自定义进度条渐变圆形的代码,希望对大家有所帮助。


猜你喜欢
- 本Demo使用三个类一个Test类一个自定义的Stack类一个自定义的Queue类可以实现的功能:1.对于一个写在文本文件中的迷宫,能够将其
- 实现闹钟运行的效果如下: 通知栏的运行后效果图如下:布局文件(activity_main.xml)<?xml version
- 1: * 的定义: * 实际上是一个类,这个类实现了特定的接口,然后将这个类在 web.xml 文件中进行描述,这样服务器在启动的时候就可
- 个人感觉mob平台功能还是比较强大的,很多功能都可以通过他们平台来实现。建议仔细观看每一个步骤,如果一个步骤没处理好,可能就会让你的这个功能
- 在spring的一个controller中要把参数传到页面,只要配置视图解析器,把参数添加到Model中,在页面用el表达式就可以取到。但是
- jwt介绍: JWT是一种用于双方之间传递安全信息的简洁的、URL安全的表述性声明规范
- 上两片第归算法学习:1)递归算法之分而治之策略2)递归算法之归并排序上一篇学习中介绍了了递归算法在排序中的一个应用:归并排序,在排序算法中还
- C# 关于Invoke首先说下,invoke和begininvoke的使用有两种情况:control中的invoke、begininvoke
- Recyclerview现在基本已经替代Listview了,RecyclerView也越来越好用了 当我们有实现条目的拖拽排序和
- 基本环境:Android studio3.6NDK:r15c(尽量使用该版本)Opencv3.4.1 android sdk操作:(1)新建
- 译文链接:https://www.infoworld.com/art...C# 在 3.0 版本中提供了对 扩展方法 的支持,扩展方法常用于
- 第一种(java8):遍历JSONArray 拼接字符串public static void main(String[] args) {JS
- 声明一个客户端对象:protected RedisClient Redis = new RedisClient("127.0.0
- 前言目前主流的锁有两种,一种是synchronized,另一种就是ReentrantLock,JDK优化到现在目前为止synchronize
- Result可以设定全局结果集,如:<struts> <constant name="struts
- 1.点击上传按钮进行如下操作,通过表单名称以及input名称获取相应的值,对于上传的文件,使用.files来获取,因为包含文件的上传,所以采
- 这一篇网络爬虫的实现就要联系上大数据了。在前两篇java实现网络爬虫和heritrix实现网络爬虫的基础上,这一次是要完整的做一次数据的收集
- 本文所述为使用WinForm相对路径时需要注意的陷阱。这类错误经常会遇到!现分析如下供大家参考。在Window系统上利用相对路径进行操作时,
- Spring depends-on的使用通过在XML中的<bean>里配置depends-on属性或者在一个类上使用注解@Dep
- RandomAccessFileRandomAccessFile 是随机访问文件(包括读/写)的类。它支持对文件随机访问的读取和写入,即我们