当前位置:网站首页>三重半圆环进度条,直接拿去就能用
三重半圆环进度条,直接拿去就能用
2022-07-06 21:09:00 【超努力的金蟾菇】
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class HalfCircleView extends View {
private int maxProgress = 100;
private float progress = 30f;
private float progress2 = 30f;
private float progress3 = 30f;
private int circleLineStrokeWidth = 50; //圆弧的宽度
private final int txtStrokeWidth = 3;
// 画圆所在的距形区域,RecF表示矩形区域,一个矩形区域对应一个椭圆,精度为Float,RECT精度为Int
private final RectF progressRectF;
private final Paint progressPaint;
private final RectF progressRectF2;
private final Paint progressPaint2;
private final RectF progressRectF3;
private final Paint progressPaint3;
public HalfCircleView(Context context, AttributeSet attrs) {
super(context, attrs);
progressRectF = new RectF();
progressPaint = new Paint();
progressRectF2 = new RectF();
progressPaint2 = new Paint();
progressRectF3 = new RectF();
progressPaint3 = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int radius = getRadius();
drawBackgroundRecf(radius);
drawProgress(canvas);
// 绘制进度百分比显示
// drawText(canvas, radius);
}
private void drawBackgroundRecf(int radius) {
// 位置
progressRectF.left = circleLineStrokeWidth / 2;
progressRectF.top = circleLineStrokeWidth / 2;
progressRectF.right = radius - circleLineStrokeWidth / 2;
progressRectF.bottom = radius - circleLineStrokeWidth / 2;
progressRectF2.left = circleLineStrokeWidth * 4;
progressRectF2.top = circleLineStrokeWidth * 4;
progressRectF2.right = radius - circleLineStrokeWidth * 4;
progressRectF2.bottom = radius - circleLineStrokeWidth * 4;
progressRectF3.left = circleLineStrokeWidth * 8;
progressRectF3.top = circleLineStrokeWidth * 8;
progressRectF3.right = radius - circleLineStrokeWidth * 8;
progressRectF3.bottom = radius - circleLineStrokeWidth * 8;
}
private int getRadius() {
int width = this.getWidth() - circleLineStrokeWidth * 2;
//因为你要画的是一个半圆,半圆一定是在一个矩形里的
//而在画半圆的时候其实是在一个正方形里绘制一个整圆的一部分
//那么我们整圆的半径就是扣除半圆进度条的宽度
return width;
}
private void drawProgress(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT); //画布颜色
progressPaint.setAntiAlias(true); //设置是否抗锯齿
progressPaint.setColor(Color.rgb(0xe9, 0xe9, 0xe9));
progressPaint.setStrokeWidth(circleLineStrokeWidth); //设置边框宽度
progressPaint.setStyle(Paint.Style.STROKE); //stroke代表中断不填充,即圆环
canvas.drawArc(progressRectF, 180, 180, false, progressPaint); // startAngel 表示圆弧起始的角度,sweepAngle表示圆弧的角度(跨度), useCenter 表示是否将中心包括在内,绘制成扇形!
if (progress > 80) {
progressPaint.setColor(Color.rgb(34,219,126)); //大于80则进度条为绿色
} else {
progressPaint.setColor(Color.rgb(247, 169, 45)); //小于80则为橙色
}
canvas.drawArc(progressRectF, 180, (progress / maxProgress) * 180, false, progressPaint);
progressPaint2.setAntiAlias(true); //设置是否抗锯齿
progressPaint2.setColor(Color.rgb(0xe9, 0xe9, 0xe9));
progressPaint2.setStrokeWidth(circleLineStrokeWidth); //设置边框宽度
progressPaint2.setStyle(Paint.Style.STROKE); //stroke代表中断不填充,即圆环
canvas.drawArc(progressRectF2, 180, 180, false, progressPaint2); // startAngel 表示圆弧起始的角度,sweepAngle表示圆弧的角度(跨度), useCenter 表示是否将中心包括在内,绘制成扇形!
if (progress2 > 80) {
progressPaint2.setColor(Color.rgb(34,219,126)); //大于80则进度条为绿色
} else {
progressPaint2.setColor(Color.rgb(247, 169, 45)); //小于80则为橙色
}
canvas.drawArc(progressRectF2, 180, (progress2 / maxProgress) * 180, false, progressPaint2);
progressPaint3.setAntiAlias(true); //设置是否抗锯齿
progressPaint3.setColor(Color.rgb(0xe9, 0xe9, 0xe9));
progressPaint3.setStrokeWidth(circleLineStrokeWidth); //设置边框宽度
progressPaint3.setStyle(Paint.Style.STROKE); //stroke代表中断不填充,即圆环
canvas.drawArc(progressRectF3, 180, 180, false, progressPaint3); // startAngel 表示圆弧起始的角度,sweepAngle表示圆弧的角度(跨度), useCenter 表示是否将中心包括在内,绘制成扇形!
if (progress3 > 80) {
progressPaint3.setColor(Color.rgb(34,219,126)); //大于80则进度条为绿色
} else {
progressPaint3.setColor(Color.rgb(247, 169, 45)); //小于80则为橙色
}
canvas.drawArc(progressRectF3, 180, (progress3 / maxProgress) * 180, false, progressPaint3);
}
private void drawText(Canvas canvas, int radius) {
progressPaint.setStrokeWidth(txtStrokeWidth);
String progressText = progress + "%";
progressPaint.setTextSize(radius / 4);
int textWidth = (int) progressPaint.measureText(progressText, 0, progressText.length());
progressPaint.setStyle(Paint.Style.FILL);
canvas.drawText(progressText, radius / 2 - textWidth / 2, radius / 2, progressPaint);
}
public void setProgress(float progress, float progress2, float progress3) {
this.progress = progress;
this.progress2 = progress2;
this.progress3 = progress3;
this.invalidate();
}
public void setProgressNotInUiThread(float progress) {
this.progress = progress;
this.postInvalidate();
}
public void setCircleLineStrokeWidth(int circleLineStrokeWidth) {
this.circleLineStrokeWidth = circleLineStrokeWidth;
this.invalidate();
}
}
边栏推荐
- CMB's written test - quantitative relationship
- Ubuntu20 installation redisjson record
- Function reentry, function overloading and function rewriting are understood by yourself
- pip只下载不安装
- 我的勇敢对线之路--详细阐述,浏览器输入URL发生了什么
- AVL树插入操作与验证操作的简单实现
- My brave way to line -- elaborate on what happens when the browser enters the URL
- Hisilicon 3559 universal platform construction: RTSP real-time playback support
- Gpt-3 is a peer review online when it has been submitted for its own research
- 小程序能运行在自有App中,且实现直播和连麦?
猜你喜欢
Gpt-3 is a peer review online when it has been submitted for its own research
VHDL implementation of single cycle CPU design
21.(arcgis api for js篇)arcgis api for js矩形采集(SketchViewModel)
Docker部署Mysql8的实现步骤
1.19.11.SQL客户端、启动SQL客户端、执行SQL查询、环境配置文件、重启策略、自定义函数(User-defined Functions)、构造函数参数
Baidu map JS development, open a blank, bmapgl is not defined, err_ FILE_ NOT_ FOUND
QT 打开文件 使用 QFileDialog 获取文件名称、内容等
Probability formula
代码质量管理
预处理——插值
随机推荐
MySQL的索引
[safe office and productivity application] Shanghai daoning provides you with onlyoffice download, trial and tutorial
链表面试常见题
VHDL implementation of arbitrary size matrix multiplication
史上最全学习率调整策略lr_scheduler
【安全攻防】序列化与反序列,你了解多少?
Kotlin Android environment construction
Que savez - vous de la sérialisation et de l'anti - séquence?
About Tolerance Intervals
ggplot 分面的细节调整汇总
数据的存储
Delete data in SQL
接口数据安全保证的10种方式
Arduino droplet detection
什么是 BA ?BA怎么样?BA和BI是什么关系?
小程序能运行在自有App中,且实现直播和连麦?
20.(arcgis api for js篇)arcgis api for js面采集(SketchViewModel)
[leetcode] 450 and 98 (deletion and verification of binary search tree)
[security attack and Defense] how much do you know about serialization and deserialization?
22.(arcgis api for js篇)arcgis api for js圆采集(SketchViewModel)