Android 自定义漂亮的圆形进度条

这几天对Android中实现画圆弧及圆弧效果中所实现的效果进行了修改,改为进度圆心进度条,效果如图所示




 




TasksCompletedView.java 代码如下

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

import com.snailws.taskscompleted.R;

/**
* @author naiyu(http://snailws.com)
* @version 1.0
*/
public class TasksCompletedView extends View {

        // 画实心圆的画笔
        private Paint mCirclePaint;
        // 画圆环的画笔
        private Paint mRingPaint;
        // 画字体的画笔
        private Paint mTextPaint;
        // 圆形颜色
        private int mCircleColor;
        // 圆环颜色
        private int mRingColor;
        // 半径
        private float mRadius;
        // 圆环半径
        private float mRingRadius;
        // 圆环宽度
        private float mStrokeWidth;
        // 圆心x坐标
        private int mXCenter;
        // 圆心y坐标
        private int mYCenter;
        // 字的长度
        private float mTxtWidth;
        // 字的高度
        private float mTxtHeight;
        // 总进度
        private int mTotalProgress = 100;
        // 当前进度
        private int mProgress;

        public TasksCompletedView(Context context, AttributeSet attrs) {
                super(context, attrs);
                // 获取自定义的属性
                initAttrs(context, attrs);
                initVariable();
        }

        private void initAttrs(Context context, AttributeSet attrs) {
                TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs,
                                R.styleable.TasksCompletedView, 0, 0);
                mRadius = typeArray.getDimension(R.styleable.TasksCompletedView_radius, 80);
                mStrokeWidth = typeArray.getDimension(R.styleable.TasksCompletedView_strokeWidth, 10);
                mCircleColor = typeArray.getColor(R.styleable.TasksCompletedView_circleColor, 0xFFFFFFFF);
                mRingColor = typeArray.getColor(R.styleable.TasksCompletedView_ringColor, 0xFFFFFFFF);
                
                mRingRadius = mRadius + mStrokeWidth / 2;
        }

        private void initVariable() {
                mCirclePaint = new Paint();
                mCirclePaint.setAntiAlias(true);
                mCirclePaint.setColor(mCircleColor);
                mCirclePaint.setStyle(Paint.Style.FILL);
                
                mRingPaint = new Paint();
                mRingPaint.setAntiAlias(true);
                mRingPaint.setColor(mRingColor);
                mRingPaint.setStyle(Paint.Style.STROKE);
                mRingPaint.setStrokeWidth(mStrokeWidth);
                
                mTextPaint = new Paint();
                mTextPaint.setAntiAlias(true);
                mTextPaint.setStyle(Paint.Style.FILL);
                mTextPaint.setARGB(255, 255, 255, 255);
                mTextPaint.setTextSize(mRadius / 2);
                
                FontMetrics fm = mTextPaint.getFontMetrics();
                mTxtHeight = (int) Math.ceil(fm.descent - fm.ascent);
                
        }

        @Override
        protected void onDraw(Canvas canvas) {

                mXCenter = getWidth() / 2;
                mYCenter = getHeight() / 2;
                
                canvas.drawCircle(mXCenter, mYCenter, mRadius, mCirclePaint);
                
                if (mProgress > 0 ) {
                        RectF oval = new RectF();
                        oval.left = (mXCenter - mRingRadius);
                        oval.top = (mYCenter - mRingRadius);
                        oval.right = mRingRadius * 2 + (mXCenter - mRingRadius);
                        oval.bottom = mRingRadius * 2 + (mYCenter - mRingRadius);
                        canvas.drawArc(oval, -90, ((float)mProgress / mTotalProgress) * 360, false, mRingPaint); //
//                        canvas.drawCircle(mXCenter, mYCenter, mRadius + mStrokeWidth / 2, mRingPaint);
                        String txt = mProgress + "%";
                        mTxtWidth = mTextPaint.measureText(txt, 0, txt.length());
                        canvas.drawText(txt, mXCenter - mTxtWidth / 2, mYCenter + mTxtHeight / 4, mTextPaint);
                }
        }
        
        public void setProgress(int progress) {
                mProgress = progress;
//                invalidate();
                postInvalidate();
        }

}

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    
    <declare-styleable name="TasksCompletedView">
        <attr name="radius" format="dimension"/>
        <attr name="strokeWidth" format="dimension"/>
        <attr name="circleColor" format="color"/>
        <attr name="ringColor" format="color"/>
    </declare-styleable>
    
</resources>

源码下载:http://download.csdn.net/detail/nainai007/6554501

  • 11
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
总共分为三层:一层为圆形边线,一层为进度边线,一层用来显示标识进度节点。 public class CircleProgressBar extends View { private int maxProgress = 100; private int progress = 15; private int progressStrokeWidth = 2; private int marxArcStorkeWidth = 16; // 画圆所在的距形区域 RectF oval; Paint paint; public CircleProgressBar(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub oval = new RectF(); paint = new Paint(); } @Override protected void onDraw(Canvas canvas) { // TODO 自动生成的方法存根 super.onDraw(canvas); int width = this.getWidth(); int height = this.getHeight(); width = (width > height) ? height : width; height = (width > height) ? height : width; paint.setAntiAlias(true); // 设置画笔为抗锯齿 paint.setColor(Color.WHITE); // 设置画笔颜色 canvas.drawColor(Color.TRANSPARENT); // 白色背景 paint.setStrokeWidth(progressStrokeWidth); // 线宽 paint.setStyle(Style.STROKE); oval.left = marxArcStorkeWidth / 2; // 左上角x oval.top = marxArcStorkeWidth / 2; // 左上角y oval.right = width - marxArcStorkeWidth / 2; // 左下角x oval.bottom = height - marxArcStorkeWidth / 2; // 右下角y canvas.drawArc(oval, -90, 360, false, paint); // 绘制白色圆圈,即进度条背景 paint.setColor(Color.rgb(0x57, 0x87, 0xb6)); paint.setStrokeWidth(marxArcStorkeWidth); canvas.drawArc(oval, -90, ((float) progress / maxProgress) * 360, false, paint); // 绘制进度圆弧,这里是蓝色 paint.setStrokeWidth(1); String text = progress + "%"; int textHeight = height / 4; paint.setTextSize(textHeight); int textWidth = (int) paint.measureText(text, 0, text.length()); paint.setStyle(Style.FILL); canvas.drawText(text, width / 2 - textWidth / 2, height / 2 + textHeight / 2, paint); } public int getMaxProgress() { return maxProgress; } public void setMaxProgress(int maxProgress) { this.maxProgress = maxProgress; } /** * 设置进度 * * @param progress * 进度百分比 * @param view * 标识进度的节点视图 */ public void setProgress(int progress, View view) { this.progress = progress; view.setAnimation(pointRotationAnima(0, (int) (((float) 360 / maxProgress) * progress))); this.invalidate(); } /** * 非UI线程调用 */ public void setProgressNotInUiThread(int progress, View view) { this.progress = progress; view.setAnimation(pointRotationAnima(0, (int) (((float) 360 / maxProgress) * progress))); this.postInvalidate(); } /** * 进度标注点的动画 * * @param fromDegrees * @param toDegrees * @return */ private Animation pointRotationAnima(float fromDegrees, float toDegrees) { int initDegress = 306;// 进度点起始位置(图片偏移约54度) RotateAnimation animation = new RotateAnimation(fromDegrees, initDegress + toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setDuration(1);// 设置动画执行时间 animation.setRepeatCount(1);// 设置重复执行次数 animation.setFillAfter(true);// 设置动画结束后是否停留在结束位置 return animation; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值