Android自定義圓形系統(tǒng)音量條

由于最近項目需求,需要把原生的音量條換成圓形的,剛開始采用系統(tǒng)的ProgressBar,沒有達(dá)到預(yù)想的效果,所以決定采用自定義的View來實(shí)現(xiàn)。先看下效果圖:

GIF.gif

概述:

1.首先自定義View的屬性,畫出圓環(huán)形狀

2.找到framework中音量條顯示的代碼并替換成自定義的布局

自定義圓形音量條的流程:

1.需要繼承Android中的View,并且初始化圓形進(jìn)度條需要的一些顏色值等。

    private int mRoundColor = Color.parseColor("#ccffffff");//20%白色透明,圓環(huán)背景色
    private int mRroundProgressColor = Color.parseColor("#00b4ff");//藍(lán)色,圓環(huán)進(jìn)度顏色
    private float mRoundWidth = 25;//圓環(huán)寬度
    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.畫出最外圈圓環(huán)

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

3.畫出最里面圓環(huán)

        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.設(shè)置圓環(huán)的進(jìn)度顯示

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

至此,自定義圓環(huán)進(jìn)度條就完成,代碼很簡單,最主要的是要找到系統(tǒng)音量條顯示的代碼位置進(jìn)行替換。

修改系統(tǒng)音量條顯示

通過查看系統(tǒng)音量條的調(diào)用流程可以找到這段代碼的位置SystemUI/src/com/android/systemui/volume/VolumePanel.java

1.VolumePanel類里面主要是設(shè)置音量條的顯示布局,首先我們要看清楚要設(shè)置的音量類型,private enum StreamResources里面就展現(xiàn)出來系統(tǒng)音量的幾種類型,包括鈴聲,藍(lán)牙,音樂等類型的音量條。
2.接下來就需要初始化我們自定義的圓形進(jìn)度條了,VolumePanel(Context context, ZenModeController zenController)在這個方法和createSliders()方法中去初始化我們要加載的布局文件,同時我們還可以自定義顯示的位置大小等。
3.最后,在updateSliderProgress()方法中設(shè)置音量值。

至此,自定義圓環(huán)音量條已經(jīng)介紹完畢。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容