当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢
[dpdk] dpdk sample source code analysis III: dpdk-l3fwd_ 001
Some thoughts on cross end development of kbone and applet
Probability formula
Codeworks 5 questions per day (1700 average) - day 7
map和set的实现
Top 50 hit industry in the first half of 2022
Mobile measurement and depth link platform - Branch
VHDL implementation of arbitrary size matrix multiplication
ABAP 動態內錶分組循環
【OA】Excel 文档生成器: Openpyxl 模块
随机推荐
Arduino droplet detection
codeforces每日5题(均1700)-第七天
golang 压缩和解压zip文件
My brave way to line -- elaborate on what happens when the browser enters the URL
杭州电 3711 Binary Number
It's too convenient. You can complete the code release and approval by nailing it!
Introduction to opensea platform developed by NFT trading platform (I)
Summer 2022 daily question 1 (1)
Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
一些常用软件相关
Create commonly used shortcut icons at the top of the ad interface (menu bar)
SSL certificate deployment
使用 TiDB Lightning 恢复 GCS 上的备份数据
opencv第三方库
2022中青杯C题城市交通思路分析
One of oscp tools: dirsearch usage Encyclopedia
QT 打开文件 使用 QFileDialog 获取文件名称、内容等
Termux set up the computer to connect to the mobile phone. (knock the command quickly), mobile phone termux port 8022
Simple implementation of AVL tree insertion and verification operations
Tencent cloud native database tdsql-c was selected into the cloud native product catalog of the Academy of communications and communications