进度条

public class MainActivity extends Activity {

	private CircleProgress cp;
	private ImageView iv;
	int finish = 80;
	private int centerX,centerY;
	private int textSize = 38;
	private int roundWidth = 14;
	private float finished =80,unfinished = 20;
	Paint paint;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_activity);
		cp = (CircleProgress) this.findViewById(R.id.cp_home);
		iv=(ImageView) this.findViewById(R.id.iv);
		if(iv == null){
			Log.v("iv", "null");
			return;
		}
		drawProgress();
	}
	
	public void drawCircle(View view){
		cp.setProgress(finish,100 - finish);
		finish--;
	}
	
	private void drawProgress(){
		Bitmap bitmap1 = null;
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.chart_bg).
						copy(Bitmap.Config.ARGB_8888, true);
		bitmap1 = Bitmap.createBitmap(bitmap);
		centerX = bitmap1.getWidth()/2;
		centerY = bitmap1.getHeight()/2;
		Canvas canvas = new Canvas(bitmap1);
		paint = new Paint();
		drawText(canvas);
		drawTaskFinish(canvas);
		drawTaskUnfinish(canvas);
		iv.setImageBitmap(bitmap1);
	}
	private void drawText(Canvas canvas){
		//半径
		int radius = (int)(centerX - roundWidth/2);
		paint.setColor(Color.parseColor("#3061AE"));
		paint.setStrokeWidth(0); 
		paint.setTextSize(textSize);
		paint.setTypeface(Typeface.DEFAULT_BOLD); //设置字体
		int percent = (int)((finished / (finished + unfinished)) * 100);
		float textWidth = paint.measureText(percent + "%");
		canvas.drawText(percent + "%", centerX - textWidth / 2, centerY + textSize/3, paint);
		
		paint.setStrokeWidth(roundWidth); //设置圆环的宽度
		paint.setColor(Color.parseColor("#FF721A"));  //设置进度的颜色
		paint.setStyle(Style.STROKE);
		RectF oval = new RectF(centerX - radius, centerY - radius, centerX
				+ radius, centerY + radius);
		if(percent >0){
			//根据进度画圆弧,同时改变起始角度,进行绘制
			canvas.drawArc(oval, -90, 360 * percent/100, false, paint); 
		}
	}
	/**
	 * 绘制完成任务的形状
	 * @param canvas
	 */
	private void drawTaskFinish(Canvas canvas){
		if(finished <=0){
			return;
		}
		int centerFinishX = (int) (centerX + centerX * 0.6);
		paint.setColor(Color.parseColor("#FF721A"));
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		canvas.drawCircle(centerFinishX, centerFinishX, 20, paint);
		
		paint.setColor(Color.WHITE);
		paint.setStrokeWidth(0); 
		paint.setTextSize(12);
		paint.setTypeface(Typeface.DEFAULT_BOLD);
		float textWidth = paint.measureText(finished + "万");
		canvas.drawText(finished + "万", centerFinishX - textWidth / 2,
						centerFinishX + 6, paint);
	}
	
	/**
	 * 绘制未完成的任务图形
	 * @param canvas
	 */
	private void drawTaskUnfinish(Canvas canvas){
		if(unfinished <=0){
			return;
		}
		int centerUnfinishX = (int) (centerX - centerX * 0.6);
		paint.setColor(Color.parseColor("#1FC2F3"));
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		canvas.drawCircle(centerUnfinishX, centerUnfinishX, 20, paint);
		
		paint.setColor(Color.WHITE);
		paint.setStrokeWidth(0); 
		paint.setTextSize(12);
		paint.setTypeface(Typeface.DEFAULT_BOLD);
		float textWidth = paint.measureText(unfinished + "万");
		canvas.drawText(unfinished + "万", centerUnfinishX - textWidth / 2,
						centerUnfinishX + 6, paint);
	}
	
}

 

posted @ 2015-05-18 21:05  静以养身 俭以养德  阅读(131)  评论(0编辑  收藏  举报