当前位置:网站首页>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();

    }

}


原网站

版权声明
本文为[Super hard Golden Toad mushroom]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207062108568633.html