Android圆形进度条

/**
 * 仿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;

    /**
     * 用于定义的圆弧的形状和大小的界限
     */
    private RectF oval;

    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();
        oval = new RectF();

        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); //画出圆环

        /*
         * 画进度百分比 
         */
        paint.setStrokeWidth(0);
        paint.setColor(textColor);
        paint.setTextSize(textSize);
        paint.setTypeface(Typeface.SANS_SERIF); //设置字体
        int percent = (int) (((float) progress / (float) max) * 100);  //中间的进度百分比,先转换成float在进行除法运算,不然都为0
        float textWidth = paint.measureText(percent + "%");   //测量字体宽度,我们需要根据字体的宽度设置在圆环中间  

        if (textIsDisplayable && percent != 0 && style == STROKE) {
            canvas.drawText(percent + "%", (getWidth() - paint.measureText(percent + "%")) / 2.0f,
                    (getWidth() - (paint.descent() + paint.ascent())) / 2.0f, paint);
        }

        /*
         * 画圆弧 ,画圆环的进度 
         */

        //设置进度是实心还是空心  
        paint.setStrokeWidth(roundWidth); //设置圆环的宽度
        paint.setColor(roundProgressColor);  //设置进度的颜色
        oval.set(centre - radius, centre - radius, centre + radius, centre + radius); //用于定义的圆弧的形状和大小的界限

        switch (style) {
            case STROKE: {
                paint.setStyle(Paint.Style.STROKE);
                canvas.drawArc(oval, 0 - 90, 360 * progress / max, false, paint);  //根据进度画圆弧
                break;
            }
            case FILL: {
                paint.setStyle(Paint.Style.FILL_AND_STROKE);
                if (progress != 0)
                    canvas.drawArc(oval, 0 - 90, 360 * progress / max, true, paint);  //根据进度画圆弧
                break;
            }
        }
    }


    public synchronized int getMax() {
        return max;
    }

    /**
     * 设置进度的最大值
     *
     * @param max max progress
     */
    public synchronized void setMax(int max) {
        if (max < 0) {
            throw new IllegalArgumentException("max not less than 0");
        }
        this.max = max;
    }

    /**
     * 获取进度.需要同步
     *
     * @return progress
     */
    public synchronized int getProgress() {
        return progress;
    }

    /**
     * 设置进度,此为线程安全控件,由于考虑多线程的问题,需要同步
     * 刷新界面调用postInvalidate()能在非UI线程刷新
     *
     * @param progress 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 circleColor) {
        this.roundColor = circleColor;
    }

    public int getCricleProgressColor() {
        return roundProgressColor;
    }

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

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


}  

① invalidate() :
请求重绘View树,即draw()过程。例子中他是整个刷新着UI,并且从头到尾并不会触发onMeasure()方法(控制大小用)。如果是View就重绘View,如果是ViewGroup就全部重绘。
一般引起invalidate()操作的函数如下:
1、直接调用invalidate()方法,请求重新draw(),但只会绘制调用者本身。
2、setSelection()方法 :请求重新draw(),但只会绘制调用者本身。
3、setVisibility()方法 : 当View可视状态在INVISIBLE转换VISIBLE时,会间接调用invalidate()方法,继而绘制该View。
4 、setEnabled()方法 : 请求重新draw(),但不会重新绘制任何视图包括该调用者本身。

②postInvalidate(); 与invalidate()方法区别就是,postInvalidate()方法可以在UI线程执行,也可以在工作线程执行
而invalidate()只能在UI线程操作。但是从重绘速率讲:invalidate()效率高。

③ requestLayout()
他跟invalidate()相反,他只调用measure()和layout()过程,不会调用draw()。

不会重新绘制任何视图包括该调用者本身。

④局部刷新
使用 requestFocus()方法,他只刷新你要刷新的地方。
他是让我们的某一部分获取焦点,获取焦点的会导致view的重绘。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 圆形进度条可以通过自定义 View 或者使用第三方库来实现。以下是一种简单的实现方式: 1. 创建一个自定义 View 类,继承自 View 或者 ProgressBar。 2. 在自定义 View 的构造方法中初始化画笔和属性。 3. 重写 onDraw 方法,在该方法中绘制圆形进度条。 4. 使用属性动画或者定时器来更新进度值,并调用 invalidate 方法触发重绘。 下面是一个简单的示例代码: ```java public class CircleProgressBar extends View { private Paint backgroundPaint; private Paint progressPaint; private RectF arcRect; private int progress; private int maxProgress; public CircleProgressBar(Context context) { super(context); init(); } public CircleProgressBar(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { backgroundPaint = new Paint(); backgroundPaint.setColor(Color.GRAY); backgroundPaint.setStyle(Paint.Style.STROKE); backgroundPaint.setStrokeWidth(10); progressPaint = new Paint(); progressPaint.setStyle(Paint.Style.STROKE); progressPaint.setStrokeWidth(10); progressPaint.setStrokeCap(Paint.Cap.ROUND); arcRect = new RectF(); progress = 0; maxProgress = 100; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int centerX = getWidth() / 2; int centerY = getHeight() / 2; int radius = Math.min(centerX, centerY) - 10; arcRect.set(centerX - radius, centerY - radius, centerX + radius, centerY + radius); // 绘制背景圆弧 canvas.drawArc(arcRect, 0, 360, false, backgroundPaint); // 绘制进度圆弧 float sweepAngle = 360 * ((float) progress / maxProgress); Shader shader = new SweepGradient(centerX, centerY, Color.RED, Color.BLUE); progressPaint.setShader(shader); canvas.drawArc(arcRect, -90, sweepAngle, false, progressPaint); } public void setProgress(int progress) { this.progress = progress; invalidate(); } public void setMaxProgress(int maxProgress) { this.maxProgress = maxProgress; } } ``` 使用时,可以在布局文件中添加该自定义 View,并通过调用 `setProgress` 方法来更新进度值。 相关问题: 1. 如何创建一个自定义 View? 2. 如何绘制圆形进度条? 3. 如何使用属性动画来更新进度值? 4. 如何使用定时器来更新进度值? 5. 如何使用第三方库来实现圆形进度条
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值