Android 自定義圓形進度條

很多的時候,系統自帶的View滿足不了我們功能的需求,那麼我們就需要自己來自定義一個能滿足我們需求的View,自定義View我們需要先繼承View,添加類的構造方法,重寫父類View的一些方法,例如onDraw,爲了我們自定義的View在一個項目中能夠重用,有時候我們需要自定義其屬性,舉個很簡單的例子,我在項目中的多個界面使用我自定義的View,每個界面該自定義View的顏色都不相同,這時候如果沒有自定義屬性,那我們是不是需要構建不同顏色的View出來呢,這樣子我們的代碼就會顯得很沉厄,所以這時候我們就需要自定義其屬性來滿足我們不同的需求,自定義屬性呢,我們需要在values下建立attrs.xml文件,在其中定義我們需要定義的屬性,然後在自定義View中也要做相對應的修改,我們還是用一個小例子來看看自定義View和自定義屬性的使用


今天帶大家來自己定義一個帶進度的圓形進度條,我們還是先看一下效果吧


從上面可以看出,我們可以自定義圓環的顏色,圓環進度的顏色,是否顯示進度的百分比,進度百分比的顏色,以及進度是實心還是空心等等,這樣子是不是很多元化很方便呢?接下來我們就來教大家怎麼來定義


1.在values下面新建一個attrs.xml,現在裏面定義我們的屬性,不同的屬性對應不同的format,屬性對應的format可以參考http://blog.csdn.net/pgalxx/article/details/6766677,介紹的還是比較詳細,接下來我貼上我在自定義這個進度條所用到的屬性

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <resources>  
  3.     <declare-styleable name="RoundProgressBar">    
  4.         <attr name="roundColor" format="color"/>  
  5.         <attr name="roundProgressColor" format="color"/>  
  6.         <attr name="roundWidth" format="dimension"></attr>  
  7.         <attr name="textColor" format="color" />    
  8.         <attr name="textSize" format="dimension" />   
  9.         <attr name="max" format="integer"></attr>   
  10.         <attr name="textIsDisplayable" format="boolean"></attr>  
  11.         <attr name="style">  
  12.             <enum name="STROKE" value="0"></enum>  
  13.             <enum name="FILL" value="1"></enum>  
  14.         </attr>  
  15.     </declare-styleable>   
  16. </resources>  

2.自定義View的屬性我們算是定義好了,接下來就是怎麼獲取屬性和代碼的編寫了,我們需要在構造方法中獲取我們自己定義的相關屬性,我們先調用context.obtainStyledAttributes(attrs,R.styleable.RoundProgressBar)來獲取TypedArray,然後從TypedArray獲取我們定義的屬性,例如
  1. roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);  
  2.         roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);  
  3.         textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);  
  4.         textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);  
  5.         roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);  
  6.         max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);  
  7.         textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);  
  8.         style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);  
上面的代碼中,如roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED); getColor方法的第一個參數是我們在XML文件中定義的顏色,如果我們沒有給我們自定義的View定義顏色,他就會使用第二個參數中的默認值,即Color.RED


3.爲了方便大家理解,我將自定義View的全部代碼貼出來,裏面的代碼我也有詳細的註釋

  1. package com.example.roundprogressbar;  
  2.   
  3. import android.content.Context;  
  4. import android.content.res.TypedArray;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Color;  
  7. import android.graphics.Paint;  
  8. import android.graphics.RectF;  
  9. import android.graphics.Typeface;  
  10. import android.util.AttributeSet;  
  11. import android.util.Log;  
  12. import android.view.View;  
  13.   
  14. import com.example.circlepregress.R;  
  15.   
  16. /** 
  17.  * 仿iphone帶進度的進度條,線程安全的View,可直接在線程中更新進度 
  18.  * @author xiaanming 
  19.  * 
  20.  */  
  21. public class RoundProgressBar extends View {  
  22.     /** 
  23.      * 畫筆對象的引用 
  24.      */  
  25.     private Paint paint;  
  26.       
  27.     /** 
  28.      * 圓環的顏色 
  29.      */  
  30.     private int roundColor;  
  31.       
  32.     /** 
  33.      * 圓環進度的顏色 
  34.      */  
  35.     private int roundProgressColor;  
  36.       
  37.     /** 
  38.      * 中間進度百分比的字符串的顏色 
  39.      */  
  40.     private int textColor;  
  41.       
  42.     /** 
  43.      * 中間進度百分比的字符串的字體 
  44.      */  
  45.     private float textSize;  
  46.       
  47.     /** 
  48.      * 圓環的寬度 
  49.      */  
  50.     private float roundWidth;  
  51.       
  52.     /** 
  53.      * 最大進度 
  54.      */  
  55.     private int max;  
  56.       
  57.     /** 
  58.      * 當前進度 
  59.      */  
  60.     private int progress;  
  61.     /** 
  62.      * 是否顯示中間的進度 
  63.      */  
  64.     private boolean textIsDisplayable;  
  65.       
  66.     /** 
  67.      * 進度的風格,實心或者空心 
  68.      */  
  69.     private int style;  
  70.       
  71.     public static final int STROKE = 0;  
  72.     public static final int FILL = 1;  
  73.       
  74.     public RoundProgressBar(Context context) {  
  75.         this(context, null);  
  76.     }  
  77.   
  78.     public RoundProgressBar(Context context, AttributeSet attrs) {  
  79.         this(context, attrs, 0);  
  80.     }  
  81.       
  82.     public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {  
  83.         super(context, attrs, defStyle);  
  84.           
  85.         paint = new Paint();  
  86.   
  87.           
  88.         TypedArray mTypedArray = context.obtainStyledAttributes(attrs,  
  89.                 R.styleable.RoundProgressBar);  
  90.           
  91.         //獲取自定義屬性和默認值  
  92.         roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);  
  93.         roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);  
  94.         textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);  
  95.         textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);  
  96.         roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);  
  97.         max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);  
  98.         textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);  
  99.         style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);  
  100.           
  101.         mTypedArray.recycle();  
  102.     }  
  103.       
  104.   
  105.     @Override  
  106.     protected void onDraw(Canvas canvas) {  
  107.         super.onDraw(canvas);  
  108.           
  109.         /** 
  110.          * 畫最外層的大圓環 
  111.          */  
  112.         int centre = getWidth()/2//獲取圓心的x座標  
  113.         int radius = (int) (centre - roundWidth/2); //圓環的半徑  
  114.         paint.setColor(roundColor); //設置圓環的顏色  
  115.         paint.setStyle(Paint.Style.STROKE); //設置空心  
  116.         paint.setStrokeWidth(roundWidth); //設置圓環的寬度  
  117.         paint.setAntiAlias(true);  //消除鋸齒   
  118.         canvas.drawCircle(centre, centre, radius, paint); //畫出圓環  
  119.           
  120.         Log.e("log", centre + "");  
  121.           
  122.         /** 
  123.          * 畫進度百分比 
  124.          */  
  125.         paint.setStrokeWidth(0);   
  126.         paint.setColor(textColor);  
  127.         paint.setTextSize(textSize);  
  128.         paint.setTypeface(Typeface.DEFAULT_BOLD); //設置字體  
  129.         int percent = (int)(((float)progress / (float)max) * 100);  //中間的進度百分比,先轉換成float在進行除法運算,不然都爲0  
  130.         float textWidth = paint.measureText(percent + "%");   //測量字體寬度,我們需要根據字體的寬度設置在圓環中間  
  131.           
  132.         if(textIsDisplayable && percent != 0 && style == STROKE){  
  133.             canvas.drawText(percent + "%", centre - textWidth / 2, centre + textSize/2, paint); //畫出進度百分比  
  134.         }  
  135.           
  136.           
  137.         /** 
  138.          * 畫圓弧 ,畫圓環的進度 
  139.          */  
  140.           
  141.         //設置進度是實心還是空心  
  142.         paint.setStrokeWidth(roundWidth); //設置圓環的寬度  
  143.         paint.setColor(roundProgressColor);  //設置進度的顏色  
  144.         RectF oval = new RectF(centre - radius, centre - radius, centre  
  145.                 + radius, centre + radius);  //用於定義的圓弧的形狀和大小的界限  
  146.           
  147.         switch (style) {  
  148.         case STROKE:{  
  149.             paint.setStyle(Paint.Style.STROKE);  
  150.             canvas.drawArc(oval, 0360 * progress / max, false, paint);  //根據進度畫圓弧  
  151.             break;  
  152.         }  
  153.         case FILL:{  
  154.             paint.setStyle(Paint.Style.FILL_AND_STROKE);  
  155.             if(progress !=0)  
  156.                 canvas.drawArc(oval, 0360 * progress / max, true, paint);  //根據進度畫圓弧  
  157.             break;  
  158.         }  
  159.         }  
  160.           
  161.     }  
  162.       
  163.       
  164.     public synchronized int getMax() {  
  165.         return max;  
  166.     }  
  167.   
  168.     /** 
  169.      * 設置進度的最大值 
  170.      * @param max 
  171.      */  
  172.     public synchronized void setMax(int max) {  
  173.         if(max < 0){  
  174.             throw new IllegalArgumentException("max not less than 0");  
  175.         }  
  176.         this.max = max;  
  177.     }  
  178.   
  179.     /** 
  180.      * 獲取進度.需要同步 
  181.      * @return 
  182.      */  
  183.     public synchronized int getProgress() {  
  184.         return progress;  
  185.     }  
  186.   
  187.     /** 
  188.      * 設置進度,此爲線程安全控件,由於考慮多線的問題,需要同步 
  189.      * 刷新界面調用postInvalidate()能在非UI線程刷新 
  190.      * @param progress 
  191.      */  
  192.     public synchronized void setProgress(int progress) {  
  193.         if(progress < 0){  
  194.             throw new IllegalArgumentException("progress not less than 0");  
  195.         }  
  196.         if(progress > max){  
  197.             progress = max;  
  198.         }  
  199.         if(progress <= max){  
  200.             this.progress = progress;  
  201.             postInvalidate();  
  202.         }  
  203.           
  204.     }  
  205.       
  206.       
  207.     public int getCricleColor() {  
  208.         return roundColor;  
  209.     }  
  210.   
  211.     public void setCricleColor(int cricleColor) {  
  212.         this.roundColor = cricleColor;  
  213.     }  
  214.   
  215.     public int getCricleProgressColor() {  
  216.         return roundProgressColor;  
  217.     }  
  218.   
  219.     public void setCricleProgressColor(int cricleProgressColor) {  
  220.         this.roundProgressColor = cricleProgressColor;  
  221.     }  
  222.   
  223.     public int getTextColor() {  
  224.         return textColor;  
  225.     }  
  226.   
  227.     public void setTextColor(int textColor) {  
  228.         this.textColor = textColor;  
  229.     }  
  230.   
  231.     public float getTextSize() {  
  232.         return textSize;  
  233.     }  
  234.   
  235.     public void setTextSize(float textSize) {  
  236.         this.textSize = textSize;  
  237.     }  
  238.   
  239.     public float getRoundWidth() {  
  240.         return roundWidth;  
  241.     }  
  242.   
  243.     public void setRoundWidth(float roundWidth) {  
  244.         this.roundWidth = roundWidth;  
  245.     }  
  246.   
  247.   
  248.   
  249. }  

4.通過上面幾步我們就實現了自定義View,和自定義View的屬性,當然使用過程中還是有一點變化,我們必須在界面佈局的最頂層加上

 xmlns:android_custom="http://schemas.android.com/apk/res/com.example.circlepregress"這個即命名空間,

  • 紅色部分是自定義屬性的前綴,什麼意思呢?對於android系統控件我們定義其控件屬性是用android:XXX="XXXXXXX",而我們自己定義的就用android_custom:XXX = "XXXXXX"
  • 綠色部分則是我們的包的名字

通過上面這兩步我們就能自己定義屬性了,我貼出自定義View在XML中使用情況

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  2.     xmlns:android_custom="http://schemas.android.com/apk/res/com.example.circlepregress"  
  3.     xmlns:tools="http://schemas.android.com/tools"    
  4.     android:layout_width="match_parent"    
  5.     android:layout_height="match_parent" >    
  6.     
  7.     
  8.     <com.example.roundprogressbar.RoundProgressBar    
  9.         android:id="@+id/roundProgressBar2"    
  10.         android:layout_width="80dip"    
  11.         android:layout_height="80dip"    
  12.         android:layout_alignLeft="@+id/roundProgressBar1"    
  13.         android:layout_alignParentBottom="true"    
  14.         android:layout_marginBottom="78dp"    
  15.             
  16.             
  17.         android_custom:roundColor="#D1D1D1"    
  18.         android_custom:roundProgressColor="@android:color/black"    
  19.         android_custom:textColor="#9A32CD"    
  20.         android_custom:textIsDisplayable="false"    
  21.         android_custom:roundWidth="10dip"    
  22.         android_custom:textSize="18sp"/>    
  23. </RelativeLayout>   


原文:http://blog.csdn.net/xiaanming/article/details/10298163


項目源碼,點擊下載
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章