0
点赞
收藏
分享

微信扫一扫

Android圆形进度音量条的实现

        先看下效果图

Android圆形进度音量条的实现_自定义

概述:

1、首先自定义View的属性,画出圆环形状

2、找到framework中音量条显示的代码并替换成自定义的布局


自定义圆形音量条的流程:

1.需要继承Android中的View,并且初始化圆形进度条需要的一些颜色值等

private int mRoundColor = Color.parseColor("#ccffffff");//20%白色透明,圆环背景色
    private int mRroundProgressColor = Color.parseColor("#00b4ff");//蓝色,圆环进度颜色
    private float mRoundWidth = 25;//圆环宽度
    private int mMaxValue = 100;//最大值
    private int mCurrentPorgress;
    private int startAngle;

    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);
        mPaint = new Paint();
        startAngle = -90;
        setLayerType(View.LAYER_TYPE_HARDWARE, null);
    }

2.画出最外圈圆环

mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mRoundWidth);
        mPaint.setAntiAlias(true);
        canvas.drawCircle(center, center, radius, mPaint);

3.画出最里面圆环 

mPaint.setColor(mRroundProgressColor);
        RectF oval = new RectF(center - radius, center - radius, center
                + radius, center + radius);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawArc(oval, startAngle, 360 * mCurrentPorgress / mMaxValue,
                false, mPaint);

4.设置圆环的进度显示

public synchronized void setProgress(int progress) {
        if (progress < 0) {
            return;
        }
        if (progress > mMaxValue) {
            progress = mMaxValue;
        }
        if (progress <= mMaxValue) {
            mCurrentPorgress = progress;
            postInvalidate();
        }
    }

至此,自定义圆环进度条就完成,代码很简单,最主要的是理解自定义绘制的原理。

举报

相关推荐

0 条评论