自訂進度條

來源:互聯網
上載者:User

public class CircleView extends View { 
 
    private int maxProgress = 100; 
    private int progress = 30; 
    private int progressStrokeWidth = 4; 
    // 畫圓所在的距形地區  
    RectF oval; 
    Paint paint; 
 
    public CircleView(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        // TODO Auto-generated constructor stub  
        oval = new RectF(); 
        paint = new Paint(); 
    } 
 
    @Override 
    protected void onDraw(Canvas canvas) { 
        // TODO Auto-generated method stub  
        super.onDraw(canvas); 
        int width = this.getWidth() / 2; 
        int height = this.getHeight() / 2; 
        int len = this.getWidth() / 4; 
 
        paint.setAntiAlias(true); // 設定畫筆為消除鋸齒  
        paint.setColor(Color.WHITE); // 設定畫筆顏色  
        canvas.drawColor(Color.TRANSPARENT); // 白色背景  
        paint.setStrokeWidth(progressStrokeWidth); // 線寬  
        paint.setStyle(Style.STROKE); 
 
        oval.left = width - len; // 左上方x  
        oval.top = height - len; // 左上方y  
        oval.right = width + len; // 右下角x  
        oval.bottom = height + len; // 右下角y  
 
        paint.setColor(Color.RED); 
        canvas.drawArc(oval, -90, 360, false, paint); // 繪製白色圓圈,即進度條背景  
        paint.setColor(Color.BLUE); 
        canvas.drawArc(oval, -90, ((float) progress / maxProgress) * 360, false, paint); // 繪製進度圓弧,這裡是藍色  
 
        paint.setStrokeWidth(1); 
        String text = progress + "%"; 
        int textHeight = height / 10; // 設定字型高度  
        paint.setTextSize(textHeight); 
        int textWidth = (int) paint.measureText(text, 0, text.length()); 
        paint.setStyle(Style.FILL); 
        canvas.drawText(text, this.getWidth() / 2 - textWidth / 2, this.getHeight() / 2 + textHeight / 2, paint); 
    } 
 
    public int getMaxProgress() { 
        return maxProgress; 
    } 
 
    public void setMaxProgress(int maxProgress) { 
        this.maxProgress = maxProgress; 
    } 
 
    public void setProgress(int progress) { 
        this.progress = progress; 
        this.invalidate(); 
    } 
 
    /**
     * 非UI線程調用
     */ 
    public void setProgressNotInUiThread(int progress) { 
        this.progress = progress; 
        this.postInvalidate(); 
    } 
 

public class CircleView extends View {

 private int maxProgress = 100;
 private int progress = 30;
 private int progressStrokeWidth = 4;
 // 畫圓所在的距形地區
 RectF oval;
 Paint paint;

 public CircleView(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
  oval = new RectF();
  paint = new Paint();
 }

 @Override
 protected void onDraw(Canvas canvas) {
  // TODO Auto-generated method stub
  super.onDraw(canvas);
  int width = this.getWidth() / 2;
  int height = this.getHeight() / 2;
  int len = this.getWidth() / 4;

  paint.setAntiAlias(true); // 設定畫筆為消除鋸齒
  paint.setColor(Color.WHITE); // 設定畫筆顏色
  canvas.drawColor(Color.TRANSPARENT); // 白色背景
  paint.setStrokeWidth(progressStrokeWidth); // 線寬
  paint.setStyle(Style.STROKE);

  oval.left = width - len; // 左上方x
  oval.top = height - len; // 左上方y
  oval.right = width + len; // 右下角x
  oval.bottom = height + len; // 右下角y

  paint.setColor(Color.RED);
  canvas.drawArc(oval, -90, 360, false, paint); // 繪製白色圓圈,即進度條背景
  paint.setColor(Color.BLUE);
  canvas.drawArc(oval, -90, ((float) progress / maxProgress) * 360, false, paint); // 繪製進度圓弧,這裡是藍色

  paint.setStrokeWidth(1);
  String text = progress + "%";
  int textHeight = height / 10; // 設定字型高度
  paint.setTextSize(textHeight);
  int textWidth = (int) paint.measureText(text, 0, text.length());
  paint.setStyle(Style.FILL);
  canvas.drawText(text, this.getWidth() / 2 - textWidth / 2, this.getHeight() / 2 + textHeight / 2, paint);
 }

 public int getMaxProgress() {
  return maxProgress;
 }

 public void setMaxProgress(int maxProgress) {
  this.maxProgress = maxProgress;
 }

 public void setProgress(int progress) {
  this.progress = progress;
  this.invalidate();
 }

 /**
  * 非UI線程調用
  */
 public void setProgressNotInUiThread(int progress) {
  this.progress = progress;
  this.postInvalidate();
 }

}
MainActivity.class:

[java] 
package com.example.customview; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
 
public class MainActivity extends Activity { 
 
    private CircleView circleView; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
 
        circleView = (CircleView) findViewById(R.id.progressbar); 
        // circleView.setProgressNotInUiThread(50);  
        new Thread() 
        { 
            public void run() 
            { 
                int i = 0; 
                while (i <= 100) 
                { 
                    circleView.setProgressNotInUiThread(i); 
                    i++; 
                    try { 
                        sleep(100); 
                        if (i == 100) 
                            i = 0; 
                    } catch (InterruptedException e) { 
                        // TODO 自動產生的 catch 塊  
                        e.printStackTrace(); 
                    } 
                } 
 
            } 
        }.start(); 
 
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.main, menu); 
        return true; 
    } 
 

package com.example.customview;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

 private CircleView circleView;

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

  circleView = (CircleView) findViewById(R.id.progressbar);
  // circleView.setProgressNotInUiThread(50);
  new Thread()
  {
   public void run()
   {
    int i = 0;
    while (i <= 100)
    {
     circleView.setProgressNotInUiThread(i);
     i++;
     try {
      sleep(100);
      if (i == 100)
       i = 0;
     } catch (InterruptedException e) {
      // TODO 自動產生的 catch 塊
      e.printStackTrace();
     }
    }

   }
  }.start();

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}


activity_main.xml:


[html] 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
 
    <com.example.customview.CircleView 
        android:id="@+id/progressbar" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 
 
</RelativeLayout> 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.