首页>代码>android圆形进度条(包括仿QQ图片加载进度图)>/CircleProgress/src/com/example/roundprogressbar/RoundProgressBar.java
package com.example.roundprogressbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.example.circlepregress.R;

/**
 * ��iphone���ȵĽ�������̰߳�ȫ��View����ֱ�����߳��и��½��
 * @author xiaanming
 *
 */
public class RoundProgressBar extends View {
	/**
	 * ���ʶ��������
	 */
	private Paint paint;
	
	/**
	 * Բ������ɫ
	 */
	private int roundColor;
	
	/**
	 * Բ����ȵ���ɫ
	 */
	private int roundProgressColor;
	
	/**
	 * �м��Ȱٷֱȵ��ַ����ɫ
	 */
	private int textColor;
	
	/**
	 * �м��Ȱٷֱȵ��ַ������
	 */
	private float textSize;
	
	/**
	 * Բ���Ŀ��
	 */
	private float roundWidth;
	
	/**
	 * �����
	 */
	private int max;
	
	/**
	 * ��ǰ���
	 */
	private int progress;
	/**
	 * �Ƿ���ʾ�м�Ľ��
	 */
	private boolean textIsDisplayable;
	
	/**
	 * ��ȵķ��ʵ�Ļ��߿���
	 */
	private int style;
	
	public static final int STROKE = 0;
	public static final int FILL = 1;
	
	public RoundProgressBar(Context context) {
		this(context, null);
	}

	public RoundProgressBar(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	
	public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
		paint = new Paint();

		
		TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
				R.styleable.RoundProgressBar);
		
		//��ȡ�Զ������Ժ�Ĭ��ֵ
		roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);
		roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);
		textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);
		textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);
		roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);
		max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);
		textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);
		style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);
		
		mTypedArray.recycle();
	}
	

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		/**
		 * �������Ĵ�Բ��
		 */
		int centre = getWidth()/2; //��ȡԲ�ĵ�x���
		int radius = (int) (centre - roundWidth/2); //Բ���İ뾶
		paint.setColor(roundColor); //����Բ������ɫ
		paint.setStyle(Paint.Style.STROKE); //���ÿ���
		paint.setStrokeWidth(roundWidth); //����Բ���Ŀ��
		paint.setAntiAlias(true);  //����� 
		canvas.drawCircle(centre, centre, radius, paint); //����Բ��
		
		Log.e("log", centre + "");
		
		/**
		 * ����Ȱٷֱ�
		 */
		paint.setStrokeWidth(0); 
		paint.setColor(textColor);
		paint.setTextSize(textSize);
		paint.setTypeface(Typeface.DEFAULT_BOLD); //��������
		int percent = (int)(((float)progress / (float)max) * 100);  //�м�Ľ�Ȱٷֱȣ���ת����float�ڽ��г����㣬��Ȼ��Ϊ0
		float textWidth = paint.measureText(percent + "%");   //���������ȣ�������Ҫ�������Ŀ��������Բ���м�
		
		if(textIsDisplayable && percent != 0 && style == STROKE){
			canvas.drawText(percent + "%", centre - textWidth / 2, centre + textSize/2, paint); //������Ȱٷֱ�
		}
		
		
		/**
		 * ��Բ�� ����Բ���Ľ��
		 */
		
		//���ý����ʵ�Ļ��ǿ���
		paint.setStrokeWidth(roundWidth); //����Բ���Ŀ��
		paint.setColor(roundProgressColor);  //���ý�ȵ���ɫ
		RectF oval = new RectF(centre - radius, centre - radius, centre
				+ radius, centre + radius);  //���ڶ����Բ������״�ʹ�С�Ľ���
		
		switch (style) {
		case STROKE:{
			paint.setStyle(Paint.Style.STROKE);
			canvas.drawArc(oval, 0, 360 * progress / max, false, paint);  //��ݽ�Ȼ�Բ��
			break;
		}
		case FILL:{
			paint.setStyle(Paint.Style.FILL_AND_STROKE);
			if(progress !=0)
				canvas.drawArc(oval, 0, 360 * progress / max, true, paint);  //��ݽ�Ȼ�Բ��
			break;
		}
		}
		
	}
	
	
	public synchronized int getMax() {
		return max;
	}

	/**
	 * ���ý�ȵ����ֵ
	 * @param max
	 */
	public synchronized void setMax(int max) {
		if(max < 0){
			throw new IllegalArgumentException("max not less than 0");
		}
		this.max = max;
	}

	/**
	 * ��ȡ���.��Ҫͬ��
	 * @return
	 */
	public synchronized int getProgress() {
		return progress;
	}

	/**
	 * ���ý�ȣ���Ϊ�̰߳�ȫ�ؼ������ڿ��Ƕ��ߵ����⣬��Ҫͬ��
	 * ˢ�½������postInvalidate()���ڷ�UI�߳�ˢ��
	 * @param progress
	 */
	public synchronized void setProgress(int progress) {
		if(progress < 0){
			throw new IllegalArgumentException("progress not less than 0");
		}
		if(progress > max){
			progress = max;
		}
		if(progress <= max){
			this.progress = progress;
			postInvalidate();
		}
		
	}
	
	
	public int getCricleColor() {
		return roundColor;
	}

	public void setCricleColor(int cricleColor) {
		this.roundColor = cricleColor;
	}

	public int getCricleProgressColor() {
		return roundProgressColor;
	}

	public void setCricleProgressColor(int cricleProgressColor) {
		this.roundProgressColor = cricleProgressColor;
	}

	public int getTextColor() {
		return textColor;
	}

	public void setTextColor(int textColor) {
		this.textColor = textColor;
	}

	public float getTextSize() {
		return textSize;
	}

	public void setTextSize(float textSize) {
		this.textSize = textSize;
	}

	public float getRoundWidth() {
		return roundWidth;
	}

	public void setRoundWidth(float roundWidth) {
		this.roundWidth = roundWidth;
	}



}
最近下载更多
浆糊尛虾米  LV1 2022年8月30日
chire169  LV1 2022年1月7日
lin1218  LV1 2020年11月2日
zl201906  LV2 2019年6月17日
ghmghm  LV1 2018年12月4日
jiangxiaolong  LV1 2018年3月13日
haocheng  LV2 2017年6月23日
hugh1234  LV1 2017年1月18日
david_taotao  LV10 2017年1月6日
wjzn  LV2 2016年12月26日
最近浏览更多
李莉182828 2024年2月21日
暂无贡献等级
暂无贡献等级
浆糊尛虾米  LV1 2022年8月30日
bingpine  LV6 2022年5月10日
xierongsong  LV1 2021年11月28日
ewewrfas  LV2 2021年6月1日
sizeking  LV8 2021年3月15日
natism  LV9 2020年12月3日
lin1218  LV1 2020年11月2日
user666 2020年9月30日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友