当前位置:网站首页>Triple half circle progress bar, you can use it directly
Triple half circle progress bar, you can use it directly
2022-07-07 04:02:00 【Super hard Golden Toad mushroom】
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; // The width of the arc
private final int txtStrokeWidth = 3;
// The distance area where the circle is drawn ,RecF Represents a rectangular area , A rectangular area corresponds to an ellipse , Accuracy of Float,RECT Accuracy of 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);
// Draw progress percentage display
// drawText(canvas, radius);
}
private void drawBackgroundRecf(int radius) {
// Location
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;
// Because what you want to draw is a semicircle , The semicircle must be in a rectangle
// When drawing a semicircle, you are actually drawing a part of a whole circle in a square
// Then the radius of our whole circle is the width of the half circle progress bar
return width;
}
private void drawProgress(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT); // Canvas Color
progressPaint.setAntiAlias(true); // Set whether anti aliasing
progressPaint.setColor(Color.rgb(0xe9, 0xe9, 0xe9));
progressPaint.setStrokeWidth(circleLineStrokeWidth); // Set border width
progressPaint.setStyle(Paint.Style.STROKE); //stroke Means interrupt does not fill , Ring
canvas.drawArc(progressRectF, 180, 180, false, progressPaint); // startAngel Represents the starting angle of the arc ,sweepAngle Represents the angle of the arc ( span ), useCenter Indicates whether the center is included , Draw as a fan !
if (progress > 80) {
progressPaint.setColor(Color.rgb(34,219,126)); // Greater than 80 Then the progress bar is green
} else {
progressPaint.setColor(Color.rgb(247, 169, 45)); // Less than 80 And it can reveal which lines of code may need further testing
}
canvas.drawArc(progressRectF, 180, (progress / maxProgress) * 180, false, progressPaint);
progressPaint2.setAntiAlias(true); // Set whether anti aliasing
progressPaint2.setColor(Color.rgb(0xe9, 0xe9, 0xe9));
progressPaint2.setStrokeWidth(circleLineStrokeWidth); // Set border width
progressPaint2.setStyle(Paint.Style.STROKE); //stroke Means interrupt does not fill , Ring
canvas.drawArc(progressRectF2, 180, 180, false, progressPaint2); // startAngel Represents the starting angle of the arc ,sweepAngle Represents the angle of the arc ( span ), useCenter Indicates whether the center is included , Draw as a fan !
if (progress2 > 80) {
progressPaint2.setColor(Color.rgb(34,219,126)); // Greater than 80 Then the progress bar is green
} else {
progressPaint2.setColor(Color.rgb(247, 169, 45)); // Less than 80 And it can reveal which lines of code may need further testing
}
canvas.drawArc(progressRectF2, 180, (progress2 / maxProgress) * 180, false, progressPaint2);
progressPaint3.setAntiAlias(true); // Set whether anti aliasing
progressPaint3.setColor(Color.rgb(0xe9, 0xe9, 0xe9));
progressPaint3.setStrokeWidth(circleLineStrokeWidth); // Set border width
progressPaint3.setStyle(Paint.Style.STROKE); //stroke Means interrupt does not fill , Ring
canvas.drawArc(progressRectF3, 180, 180, false, progressPaint3); // startAngel Represents the starting angle of the arc ,sweepAngle Represents the angle of the arc ( span ), useCenter Indicates whether the center is included , Draw as a fan !
if (progress3 > 80) {
progressPaint3.setColor(Color.rgb(34,219,126)); // Greater than 80 Then the progress bar is green
} else {
progressPaint3.setColor(Color.rgb(247, 169, 45)); // Less than 80 And it can reveal which lines of code may need further testing
}
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();
}
}
边栏推荐
猜你喜欢
Do you choose pandas or SQL for the top 1 of data analysis in your mind?
Kbone与小程序跨端开发的一些思考
Storage of data
Operational amplifier application summary 1
A 股指数成分数据 API 数据接口
【开发软件】 tilipa开发者软件
map和set的实现
数据的存储
Top 50 hit industry in the first half of 2022
Adaptive non European advertising retrieval system amcad
随机推荐
2022年上半年HIT行业TOP50
QT 项目 表格新建列名称设置 需求练习(找数组消失的数字、最大值)
手机号国际区号JSON格式另附PHP获取
[security attack and Defense] how much do you know about serialization and deserialization?
杭州电 3711 Binary Number
On file uploading of network security
The JSON format of the international area code of the mobile phone number is obtained with PHP
你心目中的数据分析 Top 1 选 Pandas 还是选 SQL?
PHP lightweight Movie Video Search Player source code
机器学习笔记 - 使用机器学习进行鸟类物种分类
Mysql-数据丢失,分析binlog日志文件
使用 BR 恢复 GCS 上的备份数据
How to detect whether the MySQL code runs deadlock +binlog view
Quick completion guide of manipulator (10): accessible workspace
预处理——插值
Some thoughts on cross end development of kbone and applet
Force buckle ----- path sum III
codeforces每日5题(均1700)-第七天
Codeworks 5 questions per day (1700 average) - day 7
Implementation of map and set