Android -- SeekBar(II)縱向seekBar

花追雨發表於2020-12-05

Android – SeekBar(II)縱向seekBar

效果:
在這裡插入圖片描述

佈局:

<RelativeLayout
	android:layout_width="120dp"
    android:layout_height="120dp"
    android:layout_margin="@dimen/margin_10"
    android:padding="10dp">

    <com.nepalese.toollibs.Activity.Component.VerticalSeekBar
            android:id="@+id/vSeekBar"
            android:padding="@dimen/padding_5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:progressTint="@color/colorRed"
            android:thumbTint="@color/colorYellow"/>

     <TextView
            android:id="@+id/tvVSB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorBlack"
            android:padding="@dimen/padding_5"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"/>
</RelativeLayout>

使用方法:(與橫向的一樣)

private VerticalSeekBar verticalSeekBar;
private TextView tvVSB;

verticalSeekBar = findViewById(R.id.vSeekBar);
tvVSB = findViewById(R.id.tvVSB);

//設定最大值
 verticalSeekBar.setMax(100);

//設定監聽
verticalSeekBar.setOnVerticalSeekBarChangeListener(new VerticalSeekBar.OnVerticalSeekBarChangeListener() {
            @Override
            public void onProgressChanged(VerticalSeekBar var1, int progress, boolean fromUser) {
                tvVSB.setText(""+progress);
            }

            @Override
            public void onStartTrackingTouch(VerticalSeekBar var1) {
                tvVSB.setText("StartTracking");
            }

            @Override
            public void onStopTrackingTouch(VerticalSeekBar var1) {
                tvVSB.setText(" "+var1.getProgress());
            }
        });

原始碼:

public class VerticalSeekBar extends android.support.v7.widget.AppCompatSeekBar {
    private static final String TAG = "VerticalSeekBar";

    public VerticalSeekBar(Context context) {
        super(context);
    }

    private int process;//進度
    private OnVerticalSeekBarChangeListener listener;//回撥監聽

    public void setOnVerticalSeekBarChangeListener(OnVerticalSeekBarChangeListener verticalSeekBarChangeListener){
        listener = verticalSeekBarChangeListener;
    }

    public VerticalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    protected void onDraw(Canvas canvas) {
        //將SeekBar轉轉90度
        canvas.rotate(-90);
        //將旋轉後的檢視移動回來
        canvas.translate(-getHeight(),0);
        super.onDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                onStartTrackingTouch();
            case MotionEvent.ACTION_MOVE:
                //獲取滑動的距離
                process = getMax() - (int) (getMax() * event.getY() / getHeight());
                //設定進度
                setProgress(process);
                Log.d(TAG , "Process: " + getProgress());
                //每次拖動SeekBar都會呼叫
                onSizeChanged(getWidth(), getHeight(), 0, 0);
                onProgressChanged();
            case MotionEvent.ACTION_UP:
                onStopTrackingTouch();
                break;

            case MotionEvent.ACTION_CANCEL:
                onStopTrackingTouch();
                break;
        }
        return true;
    }

    void onProgressChanged() {
        if(listener!=null){
            listener.onProgressChanged(this,process,true);
        }
    }

    void onStartTrackingTouch() {
        if(listener!=null){
            listener.onStartTrackingTouch(this);
        }
    }

    void onStopTrackingTouch() {
        if(listener!=null){
            listener.onStopTrackingTouch(this);
        }
    }

    public interface OnVerticalSeekBarChangeListener {
        void onProgressChanged(VerticalSeekBar verticalSeekBar, int process, boolean fromUser);

        void onStartTrackingTouch(VerticalSeekBar verticalSeekBar);

        void onStopTrackingTouch(VerticalSeekBar verticalSeekBar);
    }
}

相關文章