zoukankan      html  css  js  c++  java
  • Android:获得一个竖的seekbar

        android api 自带了SeekBar,但是只是水平的;现在我想要一个竖的SeekBar,这就需要我们自己来修改SeekBar的代码来实。

    package com.dh.shoudiantong.screendatils;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.ViewConfiguration;
    import android.view.ViewParent;
    import android.widget.SeekBar;
    
    public class VerticalSeekBar extends SeekBar
    {
        private boolean mIsDragging;
        private float mTouchDownY;
        private int mScaledTouchSlop;
        private boolean isInScrollingContainer = false;
    
        public boolean isInScrollingContainer()
        {
            return isInScrollingContainer;
        }
    
        public void setInScrollingContainer(boolean isInScrollingContainer)
        {
            this.isInScrollingContainer = isInScrollingContainer;
        }
    
        /**
         * On touch, this offset plus the scaled value from the position of the
         * touch will form the progress value. Usually 0.
         */
        float mTouchProgressOffset;
    
        public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle)
        {
            super(context, attrs, defStyle);
            mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    
        }
    
        public VerticalSeekBar(Context context, AttributeSet attrs)
        {
            super(context, attrs);
        }
    
        public VerticalSeekBar(Context context)
        {
            super(context);
        }
    
        @Override
        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());
        }
    
        @Override
        protected synchronized void onDraw(Canvas canvas)
        {
            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:
                    if (isInScrollingContainer())
                    {
    
                        mTouchDownY = event.getY();
                    }
                    else
                    {
                        setPressed(true);
    
                        invalidate();
                        onStartTrackingTouch();
                        trackTouchEvent(event);
                        attemptClaimDrag();
    
                        onSizeChanged(getWidth(), getHeight(), 0, 0);
                    }
                    break;
    
                case MotionEvent.ACTION_MOVE:
                    if (mIsDragging)
                    {
                        trackTouchEvent(event);
    
                    }
                    else
                    {
                        final float y = event.getY();
                        if (Math.abs(y - mTouchDownY) > mScaledTouchSlop)
                        {
                            setPressed(true);
    
                            invalidate();
                            onStartTrackingTouch();
                            trackTouchEvent(event);
                            attemptClaimDrag();
    
                        }
                    }
                    onSizeChanged(getWidth(), getHeight(), 0, 0);
                    break;
    
                case MotionEvent.ACTION_UP:
                    if (mIsDragging)
                    {
                        trackTouchEvent(event);
                        onStopTrackingTouch();
                        setPressed(false);
    
                    }
                    else
                    {
                        // Touch up when we never crossed the touch slop threshold
                        // should
                        // be interpreted as a tap-seek to that location.
                        onStartTrackingTouch();
                        trackTouchEvent(event);
                        onStopTrackingTouch();
    
                    }
                    onSizeChanged(getWidth(), getHeight(), 0, 0);
                    // ProgressBar doesn't know to repaint the thumb drawable
                    // in its inactive state when the touch stops (because the
                    // value has not apparently changed)
                    invalidate();
                    break;
            }
            return true;
    
        }
    
        private void trackTouchEvent(MotionEvent event)
        {
            final int height = getHeight();
            final int top = getPaddingTop();
            final int bottom = getPaddingBottom();
            final int available = height - top - bottom;
    
            int y = (int) event.getY();
    
            float scale;
            float progress = 0;
    
            // 下面是最小值
            if (y > height - bottom)
            {
                scale = 0.0f;
            }
            else if (y < top)
            {
                scale = 1.0f;
            }
            else
            {
                scale = (float) (available - y + top) / (float) available;
                progress = mTouchProgressOffset;
            }
    
            final int max = getMax();
            progress += scale * max;
    
            setProgress((int) progress);
    
        }
    
        /**
         * This is called when the user has started touching this widget.
         */
        void onStartTrackingTouch()
        {
            mIsDragging = true;
        }
    
        /**
         * This is called when the user either releases his touch or the touch is
         * canceled.
         */
        void onStopTrackingTouch()
        {
            mIsDragging = false;
        }
    
        private void attemptClaimDrag()
        {
            ViewParent p = getParent();
            if (p != null)
            {
                p.requestDisallowInterceptTouchEvent(true);
            }
        }
    
        @Override
        public synchronized void setProgress(int progress)
        {
    
            super.setProgress(progress);
            onSizeChanged(getWidth(), getHeight(), 0, 0);
    
        }
    
    }
    

      这个竖的Seekbar可能还有一些问题,欢迎指正

  • 相关阅读:
    c++ --> #define中的三个特殊符号:#,##,#@
    网络通信 --> ZMQ安装和使用
    利用 mount 指令解决 Read-only file system的问题
    sed学习总结
    Verilog中锁存器与多路选择器
    Debian耳机声音问题
    MM32/STM32中断和事件梳理
    MM32 备份域学习(兼容STM32)
    有限状态机FSM(自动售报机Verilog实现)
    MM32 RTC学习(兼容STM32)
  • 原文地址:https://www.cnblogs.com/android-for-dh/p/3319400.html
Copyright © 2011-2022 走看看