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);
 }
 
 
}

更多相关文章

  1. Android(安卓)绘制波浪曲线1
  2. android绘制实心。空心五角星
  3. Android折线图
  4. Android(安卓)Paint Style
  5. Android(安卓)动画框架代码分析
  6. android 动态、静态壁纸实现
  7. Android布局优化
  8. Android(安卓)默认壁纸 简单分析
  9. SurfaceFlinger学习之路(一)View的绘制流程

随机推荐

  1. Android(安卓)从后台进入前台
  2. [Android]android studio预览视图时报错
  3. Android(安卓)录音 与播放本地音乐功能的
  4. WiFi的使用Demo
  5. Android中如何使用 address2line
  6. android 打开wifi
  7. Android(安卓)ImageSwithcher的使用
  8. Android(安卓)模拟器检测记录
  9. The Saygus VPhone V1 clears FCC, Will
  10. Android(安卓)onSaveInstanceState和onRe