自定义控件 - 圆形缓冲进度条





package com.ken_demo.appcompat.test.views;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ProgressBar;
/**
 * 圆弧进度条
 *
 * @author ken
 *
 * @2014年8月27日 @下午3:40:02
 */
public class CircleProgressView extends ProgressBar {
 
 private int myProgress = 0;
 private int radius = 50;
 private int startX = 0;
 private int startY = 0;
 private int endX = radius * 2;
 private int endY = radius * 2;
 private float strokeWidth = 10.0f;
 private float progressTextSize = 30.0f;
 
 public CircleProgressView(Context context) {
  super(context, null);
  // TODO Auto-generated constructor stub
 }
 
 public CircleProgressView(Context context, AttributeSet attrs) {
  super(context, attrs, 0);
  // TODO Auto-generated constructor stub
 }
 
 public CircleProgressView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  // TODO Auto-generated constructor stub
 }
 
 @SuppressLint("DrawAllocation")
 @Override
 protected synchronized void onDraw(Canvas canvas) {
 
  Paint paint = new Paint();
  //底纹
  paint.setAntiAlias(true);
  paint.setColor(Color.RED);
  canvas.drawCircle(radius, radius, radius, paint);
 
  //圆弧(扇形)进度条
  paint.reset();
  //设置抗锯齿
  paint.setAntiAlias(true);
  paint.setColor(Color.YELLOW);
  //设置样式{Paint.Style.STROKE, Paint.Style.FILL}
  paint.setStyle(Paint.Style.STROKE);
  //设置弧宽
  paint.setStrokeWidth(strokeWidth);
  RectF oval = new RectF(startX + strokeWidth/2, startY + strokeWidth/2, endX - strokeWidth/2, endY - strokeWidth/2);
  /**
   * oval :指定圆弧的外轮廓矩形区域。 startAngle: 圆弧起始角度,单位为度。 sweepAngle:
   * 圆弧扫过的角度,顺时针方向,单位为度。 useCenter: 如果为True时,在绘制圆弧时将圆心包括在内,通常用来绘制扇形。
   * paint: 绘制圆弧的画板属性,如颜色,是否填充等。
   */
  float startAngle = -90;
  float sweepAngle = (float)(myProgress * 360 / 100);
  canvas.drawArc(oval, startAngle, sweepAngle, false, paint);
 
  //边框
  paint.reset();
  paint.setAntiAlias(true);
  paint.setColor(Color.BLACK);
  paint.setStyle(Paint.Style.STROKE);
  paint.setStrokeWidth(2.0f);
  RectF oval2 = new RectF(startX + strokeWidth, startY + strokeWidth, endX - strokeWidth, endY - strokeWidth);
  canvas.drawOval(oval2, paint);
 
  //进度值
  paint.reset();
  paint.setAntiAlias(true);
  paint.setColor(Color.BLACK);
  paint.setTextSize(progressTextSize);
 
  float textWidth = 0.0f;
  float offsetX, offsetY;
 
  float[] widths = new float[4];
  paint.getTextWidths(myProgress + "%", widths);
  for(float f : widths){
   textWidth += f;
  }
  offsetX = textWidth/2;
  offsetY = Math.abs((paint.descent() + paint.ascent())/2);
  float x = (float)radius - offsetX;
  float y = radius + offsetY;
  canvas.drawText(myProgress + "%", x, y, paint);
 }
 
 @Override
 public synchronized void setProgress(int progress) {
  myProgress = progress;
  super.setProgress(progress);
 }
 
 
}

你可能感兴趣的:(Android,android)