VerticalSeekBar简单配置.竖向SeekBar.垂直

2022-02-23  本文已影响0人  艾希红红

VerticalSeekBar简单配置.竖向SeekBar.垂直SeekBar
主要在onDraw中,还需要配合onTouchEvent相应调整,支持onProgressChanged回调。

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;


/*******
 * VerticalSeekBar使用说明
 * 1.SeekBar不能替换成AppCompatSeekBar,防止报错可以添加@SuppressLint("AppCompatCustomView")注解
 * 2.关于方向,这里提供逆时针90度和顺时针90度的示例
 * 3.逆时针90度 onDraw--> c.rotate(-90);c.translate(-getHeight(), 0);
 * 4.顺时针90度 onDraw--> c.rotate(+90);c.translate(0, -getWidth());
 */
@SuppressLint("AppCompatCustomView")
public class VerticalSeekBar extends SeekBar {

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

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

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

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    protected void onDraw(Canvas c) {
        // 0--------100,顺时针旋转,小在上
        c.rotate(+90);
        c.translate(0, -getWidth());

//        // 0--------100,逆时针旋转,小在下
//        c.rotate(-90);
//        c.translate(-getHeight(), 0);
        super.onDraw(c);
    }

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

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                // 0--------100,顺时针旋转,小在上
                setProgress((int)(getMax() * event.getY() / getHeight()));
                onSizeChanged(getWidth(), getHeight(), 0, 0);
                // // 0--------100,逆时针旋转,小在下
                //setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
                //onSizeChanged(getWidth(), getHeight(), 0, 0);
                break;

            case MotionEvent.ACTION_CANCEL:
                break;
        }


        return true;
    }
}
<com.test.view.VerticalSeekBar
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/seekBar"
    android:max="1000"
    android:progress="0">
</com.test.view.VerticalSeekBar>
VerticalSeekBar seekBar  = view.findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        //scrollToBottom(mScrillview,terminal,progress);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
});
上一篇下一篇

猜你喜欢

热点阅读