Android音量控制設(shè)計(jì)之ProgressView

效果圖

ScreenGif4.gif
  • 可以通過(guò)旋轉(zhuǎn)view或者直接拖動(dòng)來(lái)控制進(jìn)度诲祸。
  • 根據(jù)旋轉(zhuǎn)角度,音量的移動(dòng)速度會(huì)改變
  • 根據(jù)旋轉(zhuǎn)角度,歸位時(shí)候的速度會(huì)改變

實(shí)現(xiàn)流程

  1. 重寫(xiě)onMeasure()谍婉,使得高度為外部矩形的高度+padding更啄。
  2. 重寫(xiě)onDraw(),繪制兩個(gè)矩形和一個(gè)球稚疹。
  3. 重寫(xiě)onTouchEvent()判斷是點(diǎn)擊小球移動(dòng)還是旋轉(zhuǎn)控件移動(dòng),并且判斷點(diǎn)擊是控件左半部分還是右半部分祭务,在手指抬起時(shí)内狗,執(zhí)行歸為動(dòng)畫(huà)怪嫌。
  4. 設(shè)置音量改變接口供外部使用。

使用

記得要在外層Linearlayout中要添加clipChildred=false柳沙。岩灭。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:clipChildren="false"
    tools:context=".MainActivity">

    <TextView
        android:textSize="14sp"
        android:textColor="#333333"
        android:id="@+id/tv"
        android:gravity="center"
        android:layout_marginTop="100dp"
        android:layout_marginBottom="20dp"
        android:text="音量 :"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <com.lsp.ProgressView
        android:id="@+id/voice"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

代碼

public class ProgressView extends View {
    private static final String TAG = "HappyVoiceView";
    /**
     * 外層矩形框畫(huà)筆
     */
    private Paint outRectPaint;
    /**
     * 內(nèi)層矩形框畫(huà)筆
     */
    private Paint innerRectPaint;
    /**
     * 音量控制球畫(huà)筆
     */
    private Paint ballPaint;
    /**
     * 外層,內(nèi)層赂鲤,球的矩形范圍
     */
    private RectF rectF1, rectF2, ballRect;
    /**
     * 外層矩形高度
     */
    private int outRectHeight = 50;
    /**
     * 內(nèi)層矩形框畫(huà)筆
     */
    private int innerRectHeight = 20;
    /**
     * 小球的半徑
     */
    private int circleRadius = 15;
    /**
     * 內(nèi)層小球可移動(dòng)范圍
     */
    private int length = 0;
    /**
     * 是否是旋轉(zhuǎn)控件
     */
    private boolean doRoate = false;
    /**
     * 豎直方向偏移
     */
    private float downY;
    /**
     * 角度
     */
    private int degress = 0;
    /**
     * 手指抬起歸位
     */
    private ValueAnimator valueAnimator;
    /**
     * 小球當(dāng)前位置
     */
    private int ballCurrentLength = 0;
    /**
     * 從左面旋轉(zhuǎn)控件
     */
    private boolean isTouchLeft = false;
    /**
     * 從右面面旋轉(zhuǎn)控件
     */
    private boolean isTouchBall = false;
    /**
     * 小球移動(dòng)最終速度
     */
    private int speed = 1;
    /**
     * 小球最小速度
     */
    private int minSpeed = 2;
    /**
     * 音量改變監(jiān)聽(tīng)
     */
    private OnVoiceUpdateLinstener voiceUpdateLinstener;

    public void setVoiceUpdateLinstener(OnVoiceUpdateLinstener voiceUpdateLinstener) {
        this.voiceUpdateLinstener = voiceUpdateLinstener;
    }

    public ProgressView(Context context) {
        super(context);
        init();
    }
    public ProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        setMeasuredDimension(widthSize, outRectHeight + getPaddingTop() + getPaddingBottom());

    }
    private void init() {
        outRectPaint = initPaint();
        outRectPaint.setColor(Color.CYAN);
        innerRectPaint = initPaint();
        innerRectPaint.setColor(Color.YELLOW);
        ballPaint = initPaint();
        ballPaint.setColor(Color.RED);
        ballRect = new RectF();
    }
    private void initValueAnimator() {
        valueAnimator = ValueAnimator.ofInt(degress, 0);
          valueAnimator.setDuration(Math.abs(degress)/10*100);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                degress = (int) valueAnimator.getAnimatedValue();
                invalidate();
            }
        });
        valueAnimator.start();
    }
    private Paint initPaint() {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);  //抗鋸齒
        paint.setDither(true);  //防抖動(dòng)
        paint.setColor(Color.CYAN);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeCap(Paint.Cap.SQUARE);
        return paint;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
        dealBallLength();
        rectF1 = new RectF((-getMeasuredWidth() / 2) + getPaddingLeft(), -outRectHeight / 2, (getMeasuredWidth() / 2) - getPaddingLeft(), outRectHeight / 2);
        rectF2 = new RectF((-getMeasuredWidth() / 2) + getPaddingLeft() + getPaddingLeft(), -innerRectHeight / 2, (getMeasuredWidth() / 2) - getPaddingLeft() - getPaddingLeft(), innerRectHeight / 2);
        length = (int) rectF2.width();
        canvas.rotate(degress);
        canvas.drawRoundRect(rectF1, 10, 10, outRectPaint);
        canvas.drawRoundRect(rectF2, 20, 20, innerRectPaint);
        canvas.drawCircle((rectF2.left + ballCurrentLength) + circleRadius / 2, rectF2.centerY(), circleRadius, ballPaint);
        ballRect.left = (rectF2.left + ballCurrentLength) + circleRadius / 2 - circleRadius;
        ballRect.right = (rectF2.left + ballCurrentLength) + circleRadius / 2 + circleRadius;
        ballRect.top = rectF2.centerY() - circleRadius;
        ballRect.bottom = rectF2.centerY() + circleRadius;

        if (voiceUpdateLinstener != null) {
            voiceUpdateLinstener.onVoiceChanged((int) ((float) ballCurrentLength / length * 100));
        }
    }
    private int dealBallLength() {
        speed = Math.abs(degress) / 3 + minSpeed;
        if (degress > 0 && ballCurrentLength < length) {
            speed = ballCurrentLength + speed > length ? (length - ballCurrentLength) : speed;
            ballCurrentLength += speed;
            invalidate();
        } else if (degress < 0 && ballCurrentLength > 0) {
            speed = ballCurrentLength - speed < 0 ? ballCurrentLength : speed;
            ballCurrentLength -= speed;
            invalidate();
        }

        return ballCurrentLength;
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float y = event.getY();
        float x = event.getX();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (valueAnimator != null && valueAnimator.isRunning()) {
                    return false;
                }
                if (ballRect.contains(event.getX() - getMeasuredWidth() / 2, y - getMeasuredHeight() / 2)) {
                    isTouchBall = true;
                    break;
                }
                if (rectF1.contains(event.getX() - getMeasuredWidth() / 2, y - getMeasuredHeight() / 2)) {   //平移過(guò)坐標(biāo)系
                    doRoate = true;
                    downY = (int) event.getY();
                    if (event.getX() - getMeasuredWidth() / 2 <= 0) {
                        isTouchLeft = true;
                    } else {
                        isTouchLeft = false;
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (isTouchBall) {
                    ballCurrentLength = (int) (x - getPaddingLeft() - getPaddingLeft());
                    if (ballCurrentLength < 0) {
                        ballCurrentLength = 0;
                    } else if (ballCurrentLength > length) {
                        ballCurrentLength = length;
                    }
                    invalidate();
                    break;
                }
                x = (float) Math.atan((y - downY) / rectF1.right);
                degress = (int) Math.toDegrees(x);
                degress = isTouchLeft ? -degress : degress;
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                if (doRoate) {
                    initValueAnimator();
                }
                doRoate = false;
                isTouchBall = false;
                break;
        }
        return true;
    }
    public interface OnVoiceUpdateLinstener {
        void onVoiceChanged(int voice);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末噪径,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子数初,更是在濱河造成了極大的恐慌找爱,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,430評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件泡孩,死亡現(xiàn)場(chǎng)離奇詭異车摄,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)珍德,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,406評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門(mén)练般,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人锈候,你說(shuō)我怎么就攤上這事薄料。” “怎么了泵琳?”我有些...
    開(kāi)封第一講書(shū)人閱讀 167,834評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵摄职,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我获列,道長(zhǎng)谷市,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,543評(píng)論 1 296
  • 正文 為了忘掉前任击孩,我火速辦了婚禮迫悠,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘巩梢。我一直安慰自己创泄,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,547評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布括蝠。 她就那樣靜靜地躺著鞠抑,像睡著了一般。 火紅的嫁衣襯著肌膚如雪忌警。 梳的紋絲不亂的頭發(fā)上搁拙,一...
    開(kāi)封第一講書(shū)人閱讀 52,196評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼箕速。 笑死酪碘,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的盐茎。 我是一名探鬼主播婆跑,決...
    沈念sama閱讀 40,776評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼庭呜!你這毒婦竟也來(lái)了滑进?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,671評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤募谎,失蹤者是張志新(化名)和其女友劉穎扶关,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體数冬,經(jīng)...
    沈念sama閱讀 46,221評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡节槐,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,303評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了拐纱。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片铜异。...
    茶點(diǎn)故事閱讀 40,444評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖秸架,靈堂內(nèi)的尸體忽然破棺而出揍庄,到底是詐尸還是另有隱情,我是刑警寧澤东抹,帶...
    沈念sama閱讀 36,134評(píng)論 5 350
  • 正文 年R本政府宣布蚂子,位于F島的核電站,受9級(jí)特大地震影響缭黔,放射性物質(zhì)發(fā)生泄漏食茎。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,810評(píng)論 3 333
  • 文/蒙蒙 一馏谨、第九天 我趴在偏房一處隱蔽的房頂上張望别渔。 院中可真熱鬧,春花似錦惧互、人聲如沸哎媚。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,285評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)抄伍。三九已至艘刚,卻和暖如春管宵,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,399評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工箩朴, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留岗喉,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,837評(píng)論 3 376
  • 正文 我出身青樓炸庞,卻偏偏與公主長(zhǎng)得像钱床,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子埠居,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,455評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容