Android 帶進度的圓形進度條

自定義view

 public class RoundProgressBar extends View {
        /**
         * 畫筆對象的引用
         */
        private Paint paint;
    
  
  /**
     * 圓環的顏色
     */
    private int roundColor;

    /**
     * 圓環進度的顏色
     */
    private int roundProgressColor;

    /**
     * 圓環的寬度
     */
    private float roundWidth;

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

    /**
     * 當前進度
     */
    private int progress;
    /**
     * 是否顯示中間的進度
     */
    private boolean textIsDisplayable;

    /**
     * 進度的風格,實心或者空心
     */
    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);

        //獲取自定義屬性和默認值
        roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);
        roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);
        roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);
        max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);
        textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);
        style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);

        mTypedArray.recycle();
    }


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

        /**
         * 畫最外層的大圓環
         */
        int centre = getWidth()/2; //獲取圓心的x座標
        int radius = (int) (centre - roundWidth/2); //圓環的半徑
        paint.setColor(roundColor); //設置圓環的顏色
        paint.setStyle(Paint.Style.STROKE); //設置空心
        paint.setStrokeWidth(roundWidth); //設置圓環的寬度
        paint.setAntiAlias(true);  //消除鋸齒
        canvas.drawCircle(centre, centre, radius, paint); //畫出圓環


        /**
         * 畫圓弧 ,畫圓環的進度
         */

        //設置進度是實心還是空心
        paint.setStrokeWidth(roundWidth); //設置圓環的寬度
        paint.setColor(roundProgressColor);  //設置進度的顏色
        RectF oval = new RectF(centre - radius, centre - radius, centre
                + radius, centre + radius);  //用於定義的圓弧的形狀和大小的界限

        switch (style) {
            case STROKE:{
                paint.setStyle(Paint.Style.STROKE);
                canvas.drawArc(oval, 0, 360 * progress / max, false, paint);  //根據進度畫圓弧
                break;
            }
            case FILL:{
                paint.setStyle(Paint.Style.FILL_AND_STROKE);
                if(progress !=0)
                    canvas.drawArc(oval, 0, 360 * progress / max, true, paint);  //根據進度畫圓弧
                break;
            }
        }

    }


    public synchronized int getMax() {
        return max;
    }

    /**
     * 設置進度的最大值
     * @param max
     */
    public synchronized void setMax(int max) {
        if(max < 0){
            throw new IllegalArgumentException("max not less than 0");
        }
        this.max = max;
    }

    /**
     * 獲取進度.需要同步
     * @return
     */
    public synchronized int getProgress() {
        return progress;
    }

    /**
     * 設置進度,此爲線程安全控件,由於考慮多線的問題,需要同步
     * 刷新界面調用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();
        }

    }


    public int getCricleColor() {
        return roundColor;
    }

    public void setCricleColor(int cricleColor) {
        this.roundColor = cricleColor;
    }

    public int getCricleProgressColor() {
        return roundProgressColor;
    }

    public void setCricleProgressColor(int cricleProgressColor) {
        this.roundProgressColor = cricleProgressColor;
    }



    public float getRoundWidth() {
        return roundWidth;
    }

    public void setRoundWidth(float roundWidth) {
        this.roundWidth = roundWidth;
    }

attrs.xml文件

   <declare-styleable name="RoundProgressBar">
        <attr name="roundColor" format="color" />
        <attr name="roundProgressColor" format="color" />
        <attr name="roundWidth" format="dimension"></attr>
        <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>

使用

 <com.example.myapplication.RoundProgressBar
            android:id="@+id/roundProgressBar2"
            android:layout_width="80dip"
            android:layout_height="80dip"
            android:layout_below="@+id/tasks_view"
            android:layout_centerHorizontal="true"
            app:roundColor="#D1D1D1"
            app:roundProgressColor="@android:color/black"
            app:roundWidth="10dip"
            app:textIsDisplayable="false"
           />
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章