先上效果图
public class RateTextCircularProgressBar extends FrameLayout implements CircularProgressBar.OnProgressChangeListener { private CircularProgressBar mCircularProgressBar; private TextView mRateText; public RateTextCircularProgressBar(Context activity) { super(activity); init(); } public RateTextCircularProgressBar(Context activity, AttributeSet attrs) { super(activity, attrs); init(); } int length=50; private void init() { mCircularProgressBar = new CircularProgressBar(getContext()); this.addView(mCircularProgressBar); LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); lp.width=(int) (getResources().getDisplayMetrics().density*length); lp.height=(int) (getResources().getDisplayMetrics().density*length); lp.gravity = Gravity.CENTER; mCircularProgressBar.setLayoutParams(lp); mRateText = new TextView(getContext()); this.addView(mRateText); mRateText.setLayoutParams(lp); mRateText.setGravity(Gravity.CENTER); mRateText.setTextColor(Color.BLACK); mRateText.setTextSize(11); mCircularProgressBar.setOnProgressChangeListener(this); } public void setMax( int max ) { mCircularProgressBar.setMax(max); } /** * 设置进度 * @param progress */ public void setProgress(int progress) { mCircularProgressBar.setProgress(progress); } /** * 得到 CircularProgressBar 对象,用来设置其他的一些属性 * @return */ public CircularProgressBar getCircularProgressBar() { return mCircularProgressBar; } /** * 设置中间进度百分比文字的尺寸 * @param size */ public void setTextSize(float size) { mRateText.setTextSize(size); } /** * 设置中间进度百分比文字的颜色 * @param color */ public void setTextColor( int color) { mRateText.setTextColor(color); } public void setText(String text){ mRateText.setText(text); } @Override public void onChange(int duration, int progress, float rate) { mRateText.setText("30" + "%"); } }
/** * 环形的进度条 * * @author lwz <lwz0316@gmail.com> */ public class CircularProgressBar extends View { private int mDuration = 100; private int mProgress = 40; private Paint mPaint = new Paint(); private RectF mRectF = new RectF(); private int mBackgroundColor = Color.parseColor("#F6F6F6"); private int mPrimaryColor = Color.parseColor("#2AC475"); private int mShadowColor = Color.parseColor("#4DB4B4B4"); private int mShadowColorS = Color.parseColor("#FFE1E1E1"); /** * 进度条改变监听 * <p> * {@link #onChange(int duration, int progress, float rate)} */ public interface OnProgressChangeListener { /** * 进度改变事件,当进度条进度改变,就会调用该方法 * * @param progress 当前进度 * @param rate 当前进度与总进度的商 即:rate = (float)progress / duration */ public void onChange(int duratmStrokeWidthion, int progress, float rate); } private OnProgressChangeListener mOnChangeListener; /** * 设置进度条改变监听 * * @param l */ public void setOnProgressChangeListener(OnProgressChangeListener l) { mOnChangeListener = l; } public CircularProgressBar(Context context) { super(context); } public CircularProgressBar(Context context, AttributeSet attrs) { super(context, attrs); } /** * 设置进度条的最大值, 该值要 大于 0 * * @param max */ public void setMax(int max) { if (max < 0) { max = 0; } mDuration = max; } /** * 得到进度条的最大值 * * @return */ public int getMax() { return mDuration; } /** * 设置进度条的当前的值 * * @param progress */ public void setProgress(int progress) { if (progress > mDuration) { progress = mDuration; } mProgress = progress; if (mOnChangeListener != null) { mOnChangeListener.onChange(mDuration, progress, getRateOfProgress()); } invalidate(); } /** * 得到进度条当前的值 * * @return */ public int getProgress() { return mProgress; } /** * 设置进度条背景的颜色 */ public void setBackgroundColor(int color) { mBackgroundColor = color; } /** * 设置进度条进度的颜色 */ public void setPrimaryColor(int color) { mPrimaryColor = color; } @Override protected synchronized void onDraw(Canvas canvas) { super.onDraw(canvas); float mStrokeWidth = (getResources().getDisplayMetrics().density)*7F; int length =(int) (getResources().getDisplayMetrics().density)*0; int halfWidth =(int)(((getResources().getDisplayMetrics().density)*50 - length) / 2); float halfStrokeWidth = mStrokeWidth / 2; // 设置画笔 mPaint.setColor(mBackgroundColor); mPaint.setDither(true); mPaint.setFlags(Paint.ANTI_ALIAS_FLAG); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(mStrokeWidth); mPaint.setStyle(Paint.Style.STROKE); // 画背景 canvas.drawCircle(halfWidth, halfWidth, halfWidth - halfStrokeWidth, mPaint); mPaint.setStrokeWidth(1.5f); mPaint.setColor(mShadowColorS); //内层的 canvas.drawCircle(halfWidth, halfWidth, (float) halfWidth - mStrokeWidth, mPaint); //外层的 mPaint.setColor(mShadowColor); canvas.drawCircle(halfWidth, halfWidth, (float) halfWidth, mPaint); //画进度条 mPaint.setStrokeWidth(mStrokeWidth - 4); mPaint.setColor(mPrimaryColor); halfStrokeWidth = halfStrokeWidth + 4; mRectF.top = halfStrokeWidth; mRectF.bottom = halfWidth + halfWidth - halfStrokeWidth; mRectF.left = halfStrokeWidth; mRectF.right = halfWidth + halfWidth - halfStrokeWidth; canvas.drawArc(mRectF, -90, getRateOfProgress() * 360, false, mPaint); mPaint.setColor(mPrimaryColor); canvas.save(); } private float getRateOfProgress() { return (float) mProgress / mDuration; } }