当前位置:操作系统 > 安卓/Android >>

自定义进度条

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) { 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,