Android自定義圓形ProgressBar

閑來無事做了一個自定義的進(jìn)度條,大致效果圖如下:


progressbar.gif

廢話不多說,下面直接上代碼:
自定義控件代碼CircleProgressBar.java:

public class CircleProgressBar extends View{   
  // 畫圓環(huán)的畫筆   
  private Paint ringPaint;   
  // 畫字體的畫筆   
  private Paint textPaint;   
  // 圓環(huán)顏色   
  private int ringColor;   
  // 字體顏色   
  private int textColor;   
  // 半徑   
  private float radius;   
  // 圓環(huán)寬度   
  private float strokeWidth;   
  // 字的長度   
  private float txtWidth;   
  // 字的高度   
  private float txtHeight;   
  // 總進(jìn)度   
  private int totalProgress = 100;   
  // 當(dāng)前進(jìn)度   
  private int currentProgress;   
  // 透明度   private int alpha = 25;      

  public CircleProgressBar(Context context, AttributeSet attrs) {      
    super(context, attrs);      
    initAttrs(context, attrs);      
    initVariable();   
  }   

  private void initAttrs(Context context, AttributeSet attrs) {      
    TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CircleProgressbar, 0 , 0);      
    radius = typeArray.getDimension(R.styleable.CircleProgressbar_radius, 80);      
    strokeWidth = typeArray.getDimension(R.styleable.CircleProgressbar_strokeWidth, 10);
    ringColor = typeArray.getColor(R.styleable.CircleProgressbar_ringColor, 0xFF0000);
    textColor = typeArray.getColor(R.styleable.CircleProgressbar_textColor, 0xFFFFFF);   
  }      

  private void initVariable() {      
    ringPaint = new Paint();      
    ringPaint.setAntiAlias(true);      
    ringPaint.setDither(true);      
    ringPaint.setColor(ringColor);      
    ringPaint.setStyle(Paint.Style.STROKE);      
    ringPaint.setStrokeCap(Cap.ROUND);      
    ringPaint.setStrokeWidth(strokeWidth);            
    textPaint = new Paint();      
    textPaint.setAntiAlias(true);      
    textPaint.setStyle(Paint.Style.FILL);      
    textPaint.setColor(textColor);      
    textPaint.setTextSize(radius/2);      
    FontMetrics fm = textPaint.getFontMetrics();      
    txtHeight = fm.descent + Math.abs(fm.ascent);   
  }   

  @Override   
  protected void onDraw(Canvas canvas) {      
    if (currentProgress >= 0) {         
      ringPaint.setAlpha((int) (alpha + ((float) currentProgress / totalProgress)*230));
      RectF oval = new RectF(getWidth() / 2 - radius, getHeight() / 2 - radius, getWidth() / 2 + radius, getHeight() / 2 + radius);         
      canvas.drawArc(oval, 0, 0, false, ringPaint);         
      canvas.drawArc(oval, -90, ((float) currentProgress / totalProgress) * 360, false, ringPaint);          
      String txt = currentProgress + "%";         
      txtWidth = textPaint.measureText(txt, 0, txt.length());         
      canvas.drawText(txt, getWidth() / 2 - txtWidth / 2, getHeight() / 2 + txtHeight / 4, textPaint);
    }   
  }      

  public void setProgress(int progress) {      
    currentProgress = progress;      
    postInvalidate();   
  }   
}

自定義控件的屬性文件attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>        
  <declare-styleable name="CircleProgressbar">        
    <attr name="radius" format="dimension"/>        
    <attr name="strokeWidth" format="dimension"/>        
    <attr name="ringColor" format="color"/>        
    <attr name="textColor" format="color"/>    
  </declare-styleable>    
</resources>

下面是主界面的布局文件和具體實現(xiàn):
布局activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:cp="http://schemas.android.com/apk/res-auto"    
  android:layout_width="match_parent"    
  android:layout_height="match_parent">    
  <me.my.circleprogressbar.CircleProgressBar        
    android:id="@+id/circleProgressbar"        
    android:layout_width="wrap_content"        
    android:layout_height="wrap_content"        
    android:layout_centerInParent="true"        
    cp:radius="80dp"        
    cp:strokeWidth="10dp"        
    cp:ringColor="#ff0000"        
    cp:textColor="#0000ff"/>
</RelativeLayout>

實現(xiàn)MainActivity.java:

public class MainActivity extends Activity {    
  private CircleProgressBar circleProgressBar;    
  private int totalProgress = 100;    
  private int currentProgress;    

  @Override    
  protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.activity_main);        
    circleProgressBar = (CircleProgressBar) findViewById(R.id.circleProgressbar);        
    new Thread(new ProgressRunable()).start();    
  }    

  class ProgressRunable implements Runnable {        
    @Override        
    public void run() {            
      while (currentProgress < totalProgress) {                
        currentProgress += 1;                
        circleProgressBar.setProgress(currentProgress);                
        try {                    
          Thread.sleep(100);                
        } catch (Exception e) {                    
          e.printStackTrace();                
        }            
      }        
    }    
  }
}

其實實現(xiàn)非常簡單,就是通過進(jìn)度畫圓環(huán),具體的我也就不多說了,代碼中有不足之處希望大家批評指正,共同學(xué)習(xí)!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容