欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

自定義視圖view之環(huán)形進度條

 更新時間:2023年04月04日 10:43:10   作者:小白的成長之路  
這篇文章主要介紹了自定義視圖view之環(huán)形進度條,這次介紹了4種不同的效果,直接上代碼了,需要的朋友可以參考下

 一、普通效果。

本章博客有4種不同的效果,小伙伴可以繪制更多的效果,唯一不同的代碼,是自定義view時,怎么繪制弧度。Main類和布局無更改”

這里寫圖片描述


package tester.ermu.com.pingamedemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;


/**
 * 仿iphone帶進度的進度條,線程安全的View,可直接在線程中更新進度
 * @author xiaanming
 *
 */
public class RoundProgressBar extends View {
    /**
     * 畫筆對象的引用
     */
    private Paint paint;

    /**
     * 圓環(huán)的顏色
     */
    private int roundColor;

    /**
     * 圓環(huán)進度的顏色
     */
    private int roundProgressColor;

    /**
     * 中間進度百分比的字符串的顏色
     */
    private int textColor;

    /**
     * 中間進度百分比的字符串的字體
     */
    private float textSize;

    /**
     * 圓環(huán)的寬度
     */
    private float roundWidth;

    /**
     * 最大進度
     */
    private int max;

    /**
     * 當(dāng)前進度
     */
    private int progress;
    /**
     * 是否顯示中間的進度
     */
    private boolean textIsDisplayable;

    /**
     * 進度的風(fēng)格,實心或者空心
     */
    private int style;

    public static final int STROKE = 0;
    public static final int FILL = 1;

    public RoundProgressBar(Context context) {
        this(context, null);
    }

    public RoundProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        paint = new Paint();


        TypedArray mTypedArray = context.obtainStyledAttributes(attrs,R.styleable.RoundProgressBar);

        //獲取自定義屬性和默認(rèn)值
        roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);//圓環(huán)的顏色
        roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);//圓環(huán)進度的顏色
        textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);//字體顏色
        textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);//字體大小
        roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);//圓環(huán)的寬度
        max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);//進度條最大值,一般都為1001
        textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);//是否顯示中間的進度
        style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 1);//進度的風(fēng)格,實心或者空心

        mTypedArray.recycle();
    }

    /**
     * 設(shè)置進度,并判斷進度狀態(tài)
     * 刷新界面調(diào)用postInvalidate()能在非UI線程刷新
     * @param progress
     */
    public synchronized void setProgress(int progress) {

        if(progress < 0){
            throw new IllegalArgumentException("progress not less than 0");
        }

        if(progress > max){
            progress = max;
        }

        if(progress <= max){
            this.progress = progress;
            postInvalidate();
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        /**
         * 畫最外層的大圓環(huán)
         */
        int centre = getWidth()/2; //獲取圓心的x坐標(biāo)
        Log.i("Text","centre ------1111"+centre);
        int radius = (int) (centre - roundWidth*2); //圓環(huán)的半徑


        Log.i("Text","radius ------2222"+radius);
        paint.setColor(roundColor); //設(shè)置圓環(huán)的顏色
        paint.setStyle(Paint.Style.STROKE); //設(shè)置空心
        paint.setStrokeWidth(roundWidth); //設(shè)置圓環(huán)的寬度
        paint.setAntiAlias(true);  //消除鋸齒
        canvas.drawCircle(centre, centre, radius, paint); //畫出圓環(huán)

        Log.e("log", centre + "");

        /**
         * 畫進度百分比
         */
        paint.setStrokeWidth(0);
        paint.setColor(textColor);
        paint.setTextSize(textSize);
        paint.setTypeface(Typeface.DEFAULT_BOLD); //設(shè)置字體
        int percent = (int)(((float)progress / (float)max) * 100);  //中間的進度百分比,先轉(zhuǎn)換成float在進行除法運算,不然都為0
        float textWidth = paint.measureText(percent + "%");   //測量字體寬度,我們需要根據(jù)字體的寬度設(shè)置在圓環(huán)中間

        if(textIsDisplayable && percent != 0 && style == STROKE){
            canvas.drawText(percent + "%", centre - textWidth / 2, centre + textSize/2, paint); //畫出進度百分比
        }

        /**
         * 畫圓弧 ,畫圓環(huán)的進度
         */

        //設(shè)置進度是實心還是空心
        paint.setStrokeWidth(roundWidth); //設(shè)置圓環(huán)的寬度
        paint.setColor(roundProgressColor);  //設(shè)置進度的顏色
        RectF oval = new RectF(centre - radius, centre - radius, centre+ radius, centre + radius);//----1號:代碼用于定義的圓弧的形狀和大小的界限
//      RectF oval = new RectF(centre - radius-40, centre - radius-40, centre+ radius+40, centre + radius+40);  //--2號:用于定義的圓弧的形狀和大小的界限
//      RectF ova12 = new RectF(centre - radius-60, centre - radius-60, centre+ radius+60, centre + radius+60);

//      RectF oval = new RectF(centre - radius+40, centre - radius+40, centre+ radius-40, centre + radius-40);  //--2號:用于定義的圓弧的形狀和大小的界限
//      RectF ova12 = new RectF(centre - radius+60, centre - radius+60, centre+ radius-60, centre + radius-60);

        switch (style) {

            /*
            * public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
              oval :指定圓弧的外輪廓矩形區(qū)域。
              startAngle: 圓弧起始角度,單位為度。
              sweepAngle: 圓弧掃過的角度,順時針方向,單位為度。
              useCenter: 如果為True時,在繪制圓弧時將圓心包括在內(nèi),通常用來繪制扇形。
              paint: 繪制圓弧的畫板屬性,如顏色,是否填充等。
            *
            * */
            case STROKE:{
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(18);
                paint.setColor(Color.RED);
                canvas.drawArc(oval, 0, 360 * progress / max, false, paint);  //根據(jù)進度畫圓弧

                /*最外圍黃色圓弧*/
//              paint.setStrokeWidth(9);
//              paint.setColor(Color.YELLOW);
//              canvas.drawArc(ova12, 0, 360 * progress / max, false, paint);  //根據(jù)進度畫圓弧
                break;
            }
            case FILL:{
                paint.setStyle(Paint.Style.FILL_AND_STROKE);
                paint.setStrokeWidth(18);
                paint.setColor(Color.RED);

                if(progress !=0)
                    /*最外圍紅色圓弧*/
                    canvas.drawArc(oval, 0, 360 * progress / max, true, paint);  //根據(jù)進度畫圓弧


                /*最外圍黃色圓弧*/
//              paint.setStrokeWidth(9);
//              paint.setColor(Color.YELLOW);
//              canvas.drawArc(ova12, 0, 360 * progress / max, false, paint);  //
                break;
            }
        }

    }




}

二、單環(huán)在圈外畫弧度

這里寫圖片描述

和普通效果代碼不同的地方如下,屏蔽167行,打開168行即可:

這里寫圖片描述

這里寫圖片描述

package tester.ermu.com.pingamedemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;


/**
 * 仿iphone帶進度的進度條,線程安全的View,可直接在線程中更新進度
 * @author xiaanming
 *
 */
public class RoundProgressBar extends View {
    /**
     * 畫筆對象的引用
     */
    private Paint paint;

    /**
     * 圓環(huán)的顏色
     */
    private int roundColor;

    /**
     * 圓環(huán)進度的顏色
     */
    private int roundProgressColor;

    /**
     * 中間進度百分比的字符串的顏色
     */
    private int textColor;

    /**
     * 中間進度百分比的字符串的字體
     */
    private float textSize;

    /**
     * 圓環(huán)的寬度
     */
    private float roundWidth;

    /**
     * 最大進度
     */
    private int max;

    /**
     * 當(dāng)前進度
     */
    private int progress;
    /**
     * 是否顯示中間的進度
     */
    private boolean textIsDisplayable;

    /**
     * 進度的風(fēng)格,實心或者空心
     */
    private int style;

    public static final int STROKE = 0;
    public static final int FILL = 1;

    public RoundProgressBar(Context context) {
        this(context, null);
    }

    public RoundProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        paint = new Paint();


        TypedArray mTypedArray = context.obtainStyledAttributes(attrs,R.styleable.RoundProgressBar);

        //獲取自定義屬性和默認(rèn)值
        roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);//圓環(huán)的顏色
        roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);//圓環(huán)進度的顏色
        textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);//字體顏色
        textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);//字體大小
        roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);//圓環(huán)的寬度
        max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);//進度條最大值,一般都為1001
        textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);//是否顯示中間的進度
        style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 1);//進度的風(fēng)格,實心或者空心

        mTypedArray.recycle();
    }

    /**
     * 設(shè)置進度,并判斷進度狀態(tài)
     * 刷新界面調(diào)用postInvalidate()能在非UI線程刷新
     * @param progress
     */
    public synchronized void setProgress(int progress) {

        if(progress < 0){
            throw new IllegalArgumentException("progress not less than 0");
        }

        if(progress > max){
            progress = max;
        }

        if(progress <= max){
            this.progress = progress;
            postInvalidate();
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        /**
         * 畫最外層的大圓環(huán)
         */
        int centre = getWidth()/2; //獲取圓心的x坐標(biāo)
        Log.i("Text","centre ------1111"+centre);
        int radius = (int) (centre - roundWidth*2); //圓環(huán)的半徑


        Log.i("Text","radius ------2222"+radius);
        paint.setColor(roundColor); //設(shè)置圓環(huán)的顏色
        paint.setStyle(Paint.Style.STROKE); //設(shè)置空心
        paint.setStrokeWidth(roundWidth); //設(shè)置圓環(huán)的寬度
        paint.setAntiAlias(true);  //消除鋸齒
        canvas.drawCircle(centre, centre, radius, paint); //畫出圓環(huán)

        Log.e("log", centre + "");

        /**
         * 畫進度百分比
         */
        paint.setStrokeWidth(0);
        paint.setColor(textColor);
        paint.setTextSize(textSize);
        paint.setTypeface(Typeface.DEFAULT_BOLD); //設(shè)置字體
        int percent = (int)(((float)progress / (float)max) * 100);  //中間的進度百分比,先轉(zhuǎn)換成float在進行除法運算,不然都為0
        float textWidth = paint.measureText(percent + "%");   //測量字體寬度,我們需要根據(jù)字體的寬度設(shè)置在圓環(huán)中間

        if(textIsDisplayable && percent != 0 && style == STROKE){
            canvas.drawText(percent + "%", centre - textWidth / 2, centre + textSize/2, paint); //畫出進度百分比
        }

        /**
         * 畫圓弧 ,畫圓環(huán)的進度
         */

        //設(shè)置進度是實心還是空心
        paint.setStrokeWidth(roundWidth); //設(shè)置圓環(huán)的寬度
        paint.setColor(roundProgressColor);  //設(shè)置進度的顏色
//      RectF oval = new RectF(centre - radius, centre - radius, centre+ radius, centre + radius);//----1號:代碼用于定義的圓弧的形狀和大小的界限
        RectF oval = new RectF(centre - radius-40, centre - radius-40, centre+ radius+40, centre + radius+40);  //--2號:用于定義的圓弧的形狀和大小的界限
//      RectF ova12 = new RectF(centre - radius-60, centre - radius-60, centre+ radius+60, centre + radius+60);

//      RectF oval = new RectF(centre - radius+40, centre - radius+40, centre+ radius-40, centre + radius-40);  //--2號:用于定義的圓弧的形狀和大小的界限
//      RectF ova12 = new RectF(centre - radius+60, centre - radius+60, centre+ radius-60, centre + radius-60);

        switch (style) {

            /*
            * public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
              oval :指定圓弧的外輪廓矩形區(qū)域。
              startAngle: 圓弧起始角度,單位為度。
              sweepAngle: 圓弧掃過的角度,順時針方向,單位為度。
              useCenter: 如果為True時,在繪制圓弧時將圓心包括在內(nèi),通常用來繪制扇形。
              paint: 繪制圓弧的畫板屬性,如顏色,是否填充等。
            *
            * */
            case STROKE:{
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(18);
                paint.setColor(Color.RED);
                canvas.drawArc(oval, 0, 360 * progress / max, false, paint);  //根據(jù)進度畫圓弧

                /*最外圍黃色圓弧*/
//              paint.setStrokeWidth(9);
//              paint.setColor(Color.YELLOW);
//              canvas.drawArc(ova12, 0, 360 * progress / max, false, paint);  //根據(jù)進度畫圓弧
                break;
            }
            case FILL:{
                paint.setStyle(Paint.Style.FILL_AND_STROKE);
                paint.setStrokeWidth(18);
                paint.setColor(Color.RED);

                if(progress !=0)
                    /*最外圍紅色圓弧*/
                    canvas.drawArc(oval, 0, 360 * progress / max, true, paint);  //根據(jù)進度畫圓弧


                /*最外圍黃色圓弧*/
//              paint.setStrokeWidth(9);
//              paint.setColor(Color.YELLOW);
//              canvas.drawArc(ova12, 0, 360 * progress / max, false, paint);  //
                break;
            }
        }

    }
}

三、雙環(huán)效果

這里寫圖片描述

代碼不同的地方

這里寫圖片描述

這里寫圖片描述

package tester.ermu.com.pingamedemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;


/**
 * 仿iphone帶進度的進度條,線程安全的View,可直接在線程中更新進度
 * @author xiaanming
 *
 */
public class RoundProgressBar extends View {
    /**
     * 畫筆對象的引用
     */
    private Paint paint;

    /**
     * 圓環(huán)的顏色
     */
    private int roundColor;

    /**
     * 圓環(huán)進度的顏色
     */
    private int roundProgressColor;

    /**
     * 中間進度百分比的字符串的顏色
     */
    private int textColor;

    /**
     * 中間進度百分比的字符串的字體
     */
    private float textSize;

    /**
     * 圓環(huán)的寬度
     */
    private float roundWidth;

    /**
     * 最大進度
     */
    private int max;

    /**
     * 當(dāng)前進度
     */
    private int progress;
    /**
     * 是否顯示中間的進度
     */
    private boolean textIsDisplayable;

    /**
     * 進度的風(fēng)格,實心或者空心
     */
    private int style;

    public static final int STROKE = 0;
    public static final int FILL = 1;

    public RoundProgressBar(Context context) {
        this(context, null);
    }

    public RoundProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        paint = new Paint();


        TypedArray mTypedArray = context.obtainStyledAttributes(attrs,R.styleable.RoundProgressBar);

        //獲取自定義屬性和默認(rèn)值
        roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);//圓環(huán)的顏色
        roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);//圓環(huán)進度的顏色
        textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);//字體顏色
        textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);//字體大小
        roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);//圓環(huán)的寬度
        max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);//進度條最大值,一般都為1001
        textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);//是否顯示中間的進度
        style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 1);//進度的風(fēng)格,實心或者空心

        mTypedArray.recycle();
    }

    /**
     * 設(shè)置進度,并判斷進度狀態(tài)
     * 刷新界面調(diào)用postInvalidate()能在非UI線程刷新
     * @param progress
     */
    public synchronized void setProgress(int progress) {

        if(progress < 0){
            throw new IllegalArgumentException("progress not less than 0");
        }

        if(progress > max){
            progress = max;
        }

        if(progress <= max){
            this.progress = progress;
            postInvalidate();
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        /**
         * 畫最外層的大圓環(huán)
         */
        int centre = getWidth()/2; //獲取圓心的x坐標(biāo)
        Log.i("Text","centre ------1111"+centre);
        int radius = (int) (centre - roundWidth*2); //圓環(huán)的半徑


        Log.i("Text","radius ------2222"+radius);
        paint.setColor(roundColor); //設(shè)置圓環(huán)的顏色
        paint.setStyle(Paint.Style.STROKE); //設(shè)置空心
        paint.setStrokeWidth(roundWidth); //設(shè)置圓環(huán)的寬度
        paint.setAntiAlias(true);  //消除鋸齒
        canvas.drawCircle(centre, centre, radius, paint); //畫出圓環(huán)

        Log.e("log", centre + "");

        /**
         * 畫進度百分比
         */
        paint.setStrokeWidth(0);
        paint.setColor(textColor);
        paint.setTextSize(textSize);
        paint.setTypeface(Typeface.DEFAULT_BOLD); //設(shè)置字體
        int percent = (int)(((float)progress / (float)max) * 100);  //中間的進度百分比,先轉(zhuǎn)換成float在進行除法運算,不然都為0
        float textWidth = paint.measureText(percent + "%");   //測量字體寬度,我們需要根據(jù)字體的寬度設(shè)置在圓環(huán)中間

        if(textIsDisplayable && percent != 0 && style == STROKE){
            canvas.drawText(percent + "%", centre - textWidth / 2, centre + textSize/2, paint); //畫出進度百分比
        }

        /**
         * 畫圓弧 ,畫圓環(huán)的進度
         */

        //設(shè)置進度是實心還是空心
        paint.setStrokeWidth(roundWidth); //設(shè)置圓環(huán)的寬度
        paint.setColor(roundProgressColor);  //設(shè)置進度的顏色
//      RectF oval = new RectF(centre - radius, centre - radius, centre+ radius, centre + radius);//----1號:代碼用于定義的圓弧的形狀和大小的界限
        RectF oval = new RectF(centre - radius-40, centre - radius-40, centre+ radius+40, centre + radius+40);  //--2號:用于定義的圓弧的形狀和大小的界限
        RectF ova12 = new RectF(centre - radius-60, centre - radius-60, centre+ radius+60, centre + radius+60);

//      RectF oval = new RectF(centre - radius+40, centre - radius+40, centre+ radius-40, centre + radius-40);  //--2號:用于定義的圓弧的形狀和大小的界限
//      RectF ova12 = new RectF(centre - radius+60, centre - radius+60, centre+ radius-60, centre + radius-60);

        switch (style) {

            /*
            * public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
              oval :指定圓弧的外輪廓矩形區(qū)域。
              startAngle: 圓弧起始角度,單位為度。
              sweepAngle: 圓弧掃過的角度,順時針方向,單位為度。
              useCenter: 如果為True時,在繪制圓弧時將圓心包括在內(nèi),通常用來繪制扇形。
              paint: 繪制圓弧的畫板屬性,如顏色,是否填充等。
            *
            * */
            case STROKE:{
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(18);
                paint.setColor(Color.RED);
                canvas.drawArc(oval, 0, 360 * progress / max, false, paint);  //根據(jù)進度畫圓弧

                /*最外圍黃色圓弧*/
                paint.setStrokeWidth(9);
                paint.setColor(Color.YELLOW);
                canvas.drawArc(ova12, 0, 360 * progress / max, false, paint);  //根據(jù)進度畫圓弧
                break;
            }
            case FILL:{
                paint.setStyle(Paint.Style.FILL_AND_STROKE);
                paint.setStrokeWidth(18);
                paint.setColor(Color.RED);

                if(progress !=0)
                    /*最外圍紅色圓弧*/
                    canvas.drawArc(oval, 0, 360 * progress / max, true, paint);  //根據(jù)進度畫圓弧


                /*最外圍黃色圓弧*/
                paint.setStrokeWidth(9);
                paint.setColor(Color.YELLOW);
                canvas.drawArc(ova12, 0, 360 * progress / max, false, paint);  //
                break;
            }
        }

    }




}

實心效果環(huán)形進度條

這里寫圖片描述

這里需要添加或修改的代碼如下:

這里寫圖片描述

package tester.ermu.com.pingamedemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;


/**
 * 仿iphone帶進度的進度條,線程安全的View,可直接在線程中更新進度
 * @author xiaanming
 *
 */
public class RoundProgressBar extends View {
    /**
     * 畫筆對象的引用
     */
    private Paint paint;

    /**
     * 圓環(huán)的顏色
     */
    private int roundColor;

    /**
     * 圓環(huán)進度的顏色
     */
    private int roundProgressColor;

    /**
     * 中間進度百分比的字符串的顏色
     */
    private int textColor;

    /**
     * 中間進度百分比的字符串的字體
     */
    private float textSize;

    /**
     * 圓環(huán)的寬度
     */
    private float roundWidth;

    /**
     * 最大進度
     */
    private int max;

    /**
     * 當(dāng)前進度
     */
    private int progress;
    /**
     * 是否顯示中間的進度
     */
    private boolean textIsDisplayable;

    /**
     * 進度的風(fēng)格,實心或者空心
     */
    private int style;

    public static final int STROKE = 0;
    public static final int FILL = 1;

    public RoundProgressBar(Context context) {
        this(context, null);
    }

    public RoundProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        paint = new Paint();


        TypedArray mTypedArray = context.obtainStyledAttributes(attrs,R.styleable.RoundProgressBar);

        //獲取自定義屬性和默認(rèn)值
        roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);//圓環(huán)的顏色
        roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);//圓環(huán)進度的顏色
        textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);//字體顏色
        textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);//字體大小
        roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);//圓環(huán)的寬度
        max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);//進度條最大值,一般都為1001
        textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);//是否顯示中間的進度
        style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 1);//進度的風(fēng)格,實心或者空心

        mTypedArray.recycle();
    }

    /**
     * 設(shè)置進度,并判斷進度狀態(tài)
     * 刷新界面調(diào)用postInvalidate()能在非UI線程刷新
     * @param progress
     */
    public synchronized void setProgress(int progress) {

        if(progress < 0){
            throw new IllegalArgumentException("progress not less than 0");
        }

        if(progress > max){
            progress = max;
        }

        if(progress <= max){
            this.progress = progress;
            postInvalidate();
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        /**
         * 畫最外層的大圓環(huán)
         */
        int centre = getWidth()/2; //獲取圓心的x坐標(biāo)
        Log.i("Text","centre ------1111"+centre);
        int radius = (int) (centre - roundWidth*2); //圓環(huán)的半徑


        Log.i("Text","radius ------2222"+radius);
        paint.setColor(roundColor); //設(shè)置圓環(huán)的顏色
        paint.setStyle(Paint.Style.STROKE); //設(shè)置空心
        paint.setStrokeWidth(roundWidth); //設(shè)置圓環(huán)的寬度
        paint.setAntiAlias(true);  //消除鋸齒
        canvas.drawCircle(centre, centre, radius, paint); //畫出圓環(huán)

        Log.e("log", centre + "");

        /**
         * 畫進度百分比
         */
        paint.setStrokeWidth(0);
        paint.setColor(textColor);
        paint.setTextSize(textSize);
        paint.setTypeface(Typeface.DEFAULT_BOLD); //設(shè)置字體
        int percent = (int)(((float)progress / (float)max) * 100);  //中間的進度百分比,先轉(zhuǎn)換成float在進行除法運算,不然都為0
        float textWidth = paint.measureText(percent + "%");   //測量字體寬度,我們需要根據(jù)字體的寬度設(shè)置在圓環(huán)中間

        if(textIsDisplayable && percent != 0 && style == STROKE){
            canvas.drawText(percent + "%", centre - textWidth / 2, centre + textSize/2, paint); //畫出進度百分比
        }

        /**
         * 畫圓弧 ,畫圓環(huán)的進度
         */

        //設(shè)置進度是實心還是空心
        paint.setStrokeWidth(roundWidth); //設(shè)置圓環(huán)的寬度
        paint.setColor(roundProgressColor);  //設(shè)置進度的顏色
//      RectF oval = new RectF(centre - radius, centre - radius, centre+ radius, centre + radius);//----1號:代碼用于定義的圓弧的形狀和大小的界限
//      RectF oval = new RectF(centre - radius-40, centre - radius-40, centre+ radius+40, centre + radius+40);  //--2號:用于定義的圓弧的形狀和大小的界限
//      RectF ova12 = new RectF(centre - radius-60, centre - radius-60, centre+ radius+60, centre + radius+60);

        RectF oval = new RectF(centre - radius+40, centre - radius+40, centre+ radius-40, centre + radius-40);  //--2號:用于定義的圓弧的形狀和大小的界限
        RectF ova12 = new RectF(centre - radius+60, centre - radius+60, centre+ radius-60, centre + radius-60);

        switch (style) {

            /*
            * public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
              oval :指定圓弧的外輪廓矩形區(qū)域。
              startAngle: 圓弧起始角度,單位為度。
              sweepAngle: 圓弧掃過的角度,順時針方向,單位為度。
              useCenter: 如果為True時,在繪制圓弧時將圓心包括在內(nèi),通常用來繪制扇形。
              paint: 繪制圓弧的畫板屬性,如顏色,是否填充等。
            *
            * */
            case STROKE:{
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(18);
                paint.setColor(Color.RED);
                canvas.drawArc(oval, 0, 360 * progress / max, false, paint);  //根據(jù)進度畫圓弧

                /*最外圍黃色圓弧*/
                paint.setStrokeWidth(9);
                paint.setColor(Color.YELLOW);
                canvas.drawArc(ova12, 0, 360 * progress / max, false, paint);  //根據(jù)進度畫圓弧
                break;
            }
            case FILL:{
                paint.setStyle(Paint.Style.FILL_AND_STROKE);
                paint.setStrokeWidth(18);
                paint.setColor(Color.RED);

                if(progress !=0)
                    /*最外圍紅色圓弧*/
                    canvas.drawArc(oval, 0, 360 * progress / max, true, paint);  //根據(jù)進度畫圓弧


                /*最外圍黃色圓弧*/
                paint.setStrokeWidth(9);
                paint.setColor(Color.YELLOW);
                canvas.drawArc(ova12, 0, 360 * progress / max, false, paint);  //
                break;
            }
        }

    }




}

四、Xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android_custom="http://schemas.android.com/apk/res/tester.ermu.com.pingamedemo"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#000000"
    tools:ignore="ResAuto">

    <tester.ermu.com.pingamedemo.RoundProgressBar
        android:id="@+id/roundBar"
        android:layout_width="200dip"
        android:layout_height="200dip"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="79dp"

        android_custom:roundColor="#ffffff"
        android_custom:roundProgressColor="@android:color/black"
        android_custom:textColor="#9A32CD"
        android_custom:roundWidth="15dip"
        android_custom:textSize="18sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="#ffffff"
        android:layout_margin="5dp"
        android:text="開始" />


</LinearLayout>

這里寫圖片描述

五、MainActivity中代碼的引用

package tester.ermu.com.pingamedemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity implements OnClickListener{
    private RoundProgressBar  roundBar ;
    private int progress = 0;
    private Button button1,button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cricle_progress);
        init();

    }

    private void init() {
        roundBar = (RoundProgressBar) findViewById(R.id.roundBar);

        button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case    R.id.button1:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while(progress <= 100){
                            progress += 3;
                            System.out.println(progress);
                            roundBar.setProgress(progress);
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
                break;


        }
    }
}

六、自定義屬性

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <declare-styleable name="RoundProgressBar">  
        <attr name="roundColor" format="color"/>
        <attr name="roundProgressColor" format="color"/>
        <attr name="roundWidth" format="dimension"></attr>
        <attr name="textColor" format="color" />  
        <attr name="textSize" format="dimension" /> 
        <attr name="max" format="integer"></attr> 
        <attr name="textIsDisplayable" format="boolean"></attr>
        <attr name="style">
            <enum name="STROKE" value="0"></enum>
            <enum name="FILL" value="1"></enum>
        </attr>
    </declare-styleable> 
</resources>

這里寫圖片描述

到此這篇關(guān)于自定義視圖view之環(huán)形進度條的文章就介紹到這了,更多相關(guān)自定義view環(huán)形進度條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android 模擬器(JAVA)與C++ socket 通訊 分享

    Android 模擬器(JAVA)與C++ socket 通訊 分享

    Android 模擬器(JAVA)與C++ socket 通訊 分享,需要的朋友可以參考一下
    2013-05-05
  • Activity跳轉(zhuǎn)時生命周期跟蹤的實例

    Activity跳轉(zhuǎn)時生命周期跟蹤的實例

    下面小編就為大家?guī)硪黄狝ctivity跳轉(zhuǎn)時生命周期跟蹤的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • 解決Android ListView數(shù)據(jù)為空及加載錯誤的方法

    解決Android ListView數(shù)據(jù)為空及加載錯誤的方法

    這篇文章主要為大家提供了一個解決Android ListView數(shù)據(jù)為空及加載錯誤的方法,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Android 實現(xiàn)IOS選擇拍照相冊底部彈出的實例

    Android 實現(xiàn)IOS選擇拍照相冊底部彈出的實例

    這篇文章主要介紹了Android 實現(xiàn)IOS選擇拍照相冊底部彈出的實例的相關(guān)資料,這里提供了實現(xiàn)效果圖及實現(xiàn)代碼,需要的朋友可以參考下
    2017-07-07
  • Android進階教程之ViewGroup自定義布局

    Android進階教程之ViewGroup自定義布局

    這篇文章主要給大家介紹了關(guān)于Android進階教程之ViewGroup自定義布局的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Android WebView 上傳文件支持全解析

    Android WebView 上傳文件支持全解析

    這篇文章主要針對Android WebView 上傳文件支持進行全面解析,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android 中不用線程如何實現(xiàn)倒計時

    Android 中不用線程如何實現(xiàn)倒計時

    本文給大家分享android中不用線程如何實現(xiàn)倒計時功能,非常不錯,具有參考借鑒價值,需要的朋友參考下
    2017-01-01
  • Android實現(xiàn)TCP客戶端支持讀寫操作

    Android實現(xiàn)TCP客戶端支持讀寫操作

    這篇文章主要介紹了Android-實現(xiàn)TCP客戶端,支持讀寫操作,主要是通過socket讀寫tcp,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • Android點擊事件的實現(xiàn)方式

    Android點擊事件的實現(xiàn)方式

    這篇文章主要為大家詳細(xì)介紹了Android點擊事件的實現(xiàn)方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android組件Activity的啟動過程深入分析

    Android組件Activity的啟動過程深入分析

    這篇文章主要介紹了Android組件Activity的啟動過程,Activity作為Android四大組件之一,他的啟動沒有那么簡單。這里涉及到了系統(tǒng)服務(wù)進程,啟動過程細(xì)節(jié)很多,這里我只展示主體流程。activity的啟動流程隨著版本的更替,代碼細(xì)節(jié)一直在進行更改,每次都會有很大的修改
    2023-04-04

最新評論