seekbar拇指从搜索栏移开,搜索栏拇指错位

时间:2018-03-11 19:16:46

标签: android seekbar android-seekbar

pic of wrong seekbar我在片段中使用自定义垂直搜索栏,它之前工作正常,但突然间它开始显示错误。在某些设备中,seekbar拇指放错位置,在我的情况下,我正在使用在API 21上运行的HTC欲望。 我在这里发布了我的搜索栏代码片段。

这是垂直搜索栏的xml版本

<com.sampleApp.progressbars.VerticalProgressBar
                android:id="@+id/exposureSlider"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_above="@id/icBrightnessDim"
                android:layout_below="@id/icBrightnessBright"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_marginBottom="8dp"
                android:layout_marginTop="8dp"
                android:maxWidth="50dp"
                android:minHeight="50dp"
                android:minWidth="50dp"
                android:progressDrawable="@drawable/custom_seekbar_progress"
                android:thumb="@drawable/seek_bar_thumb"
                app:ssbPopupBackgroundColor="@color/grey"
                app:ssbPopupEnabled="true"
                app:ssbPopupHeight="80dp"
                app:ssbPopupThumbColor="@color/grey"
                app:ssbPopupWidth="20dp"
                app:ssbThrottleEnabled="false"
                app:ssbTouchAreaOffset="16dp"
                />

这是垂直搜索条代码

public class VerticalProgressBar extends android.support.v7.widget.AppCompatSeekBar {

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

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

        public VerticalProgressBar(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) {
            c.rotate(-90);
            c.translate(-getHeight(), 0);

            super.onDraw(c);
        }

        private OnSeekBarChangeListener onChangeListener;
        @Override
        public void setOnSeekBarChangeListener(OnSeekBarChangeListener onChangeListener){
            this.onChangeListener = onChangeListener;
        }

        private int lastProgress = 0;
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (!isEnabled()) {
                return false;
            }

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    onChangeListener.onStartTrackingTouch(this);
                    setPressed(true);
                    setSelected(true);
                    break;
                case MotionEvent.ACTION_MOVE:
                    super.onTouchEvent(event);
                    int progress = getMax() - (int) (getMax() * event.getY() / getHeight());

                    // Ensure progress stays within boundaries
                    if(progress < 0) {progress = 0;}
                    if(progress > getMax()) {progress = getMax();}
                    setProgress(progress);  // Draw progress
                    if(progress != lastProgress) {
                        // Only enact listener if the progress has actually changed
                        lastProgress = progress;
                        onChangeListener.onProgressChanged(this, progress, true);
                    }

                    onSizeChanged(getWidth(), getHeight() , 0, 0);
                    setPressed(true);
                    setSelected(true);
                    break;
                case MotionEvent.ACTION_UP:
                    onChangeListener.onStopTrackingTouch(this);
                    setPressed(false);
                    setSelected(false);
                    break;
                case MotionEvent.ACTION_CANCEL:
                    super.onTouchEvent(event);
                    setPressed(false);
                    setSelected(false);
                    break;
            }
            return true;
        }

0 个答案:

没有答案