美文网首页
VerticalSeekBar简单配置.竖向SeekBar.垂直

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

作者: 艾希红红 | 来源:发表于2022-02-23 17:19 被阅读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) {
    
        }
    });
    

    相关文章

      网友评论

          本文标题:VerticalSeekBar简单配置.竖向SeekBar.垂直

          本文链接:https://www.haomeiwen.com/subject/ngsglrtx.html