简体   繁体   English

VerticalSeekbar在水平滚动视图中无法正常工作

[英]VerticalSeekbar not working smoothly in horizontal scroll-view

I am trying to implement a vertical seekbar in a horizontal scroll-view, 我正在尝试在水平滚动视图中实现垂直搜索栏,
but the seekbar is not working smoothly in it. 但是搜索栏无法在其中顺利运行。

This is my class for vertical sekkbar .....Source from stackoverflow only . 这是我的垂直sekkbar类..... Source仅来自stackoverflow。

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) {
        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:
                setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
                onSizeChanged(getWidth(), getHeight(), 0, 0);
                break;

            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return true;
    }
}

This is the way i am implementing seekbar . 这就是我实现seekbar的方式。

    seekbar3.setMax(5000);
      seekbar3.setProgress(mEqualizer.getBandLevel(band));
      seekbar3.setFocusable(false);
      seekbar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        horizontalscroll.setOnTouchListener(null); 
        horizontalscroll.setEnabled(false);
        mHandler.removeCallbacks(mUpdateTimeTask);
        seekbar3.setProgress(mEqualizer.getBandLevel(band));

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        //horizontalscroll.setEnabled(false);
        /*horizontalscroll.setOnTouchListener(new View.OnTouchListener(){ 
         @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true; 
            }
        });*/
        ObjectAnimator animator=ObjectAnimator.ofInt(horizontalscroll, "scrollX",8,8 );
        animator.setDuration(2000);
        animator.start();
    //  seekbar3.setProgress(mEqualizer.getBandLevel(band));
        mHandler.removeCallbacks(mUpdateTimeTask);


        // Enable Scrolling by removing the OnTouchListner
   //   horizontalscroll.setOnTouchListener(null); 
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
      boolean fromUser) {
     mEqualizer.setBandLevel(band, (short)(progress+min));
     seekbar3top.setText((progress+min)+"mB");
     seekbar3.setProgress(progress);
    // seekbar3.onDraw(c)
 /* horizontalscroll.setOnTouchListener(new View.OnTouchListener(){ 
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true; 
        }});*/
    }
   });

     }

Can anyone suggest me how to do in a simple way. 谁能建议我用简单的方法做。 My verticalseekbar doesnt move smoothly like it should move. 我的Verticalseekbar不能平稳移动。 Thanx in advance :) 提前感谢:)

Finally i solved this problem i changed my vertical seekbar class to this 最后,我解决了这个问题,我将垂直搜索栏类更改为

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

public class VerticalSeekBar extends SeekBar {
    private boolean mIsMovingThumb = false;
       static private float THUMB_SLOP = 25;
    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) {
        c.rotate(-90);
        c.translate(-getHeight(), 0);

        super.onDraw(c);
    }
    private boolean isWithinThumb(MotionEvent event) {
                final float progress = getProgress();
                final float density = this.getResources().getDisplayMetrics().density;
                final float height = getHeight();
                final float y = event.getY();
                final float max = getMax();
                if (progress >= max - (int)(max * (y + THUMB_SLOP * density) / height)
                    && progress <= max - (int)(max * (y - THUMB_SLOP * density) / height))
                    return true;
                else
                    return false;
            }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }
boolean handled=false;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (isWithinThumb(event)) {
                                       getParent().requestDisallowInterceptTouchEvent(true);
                                        mIsMovingThumb = true;
                                        handled = true;
                                    }
                                    break;
            case MotionEvent.ACTION_MOVE:

            case MotionEvent.ACTION_UP:
                                setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
                                onSizeChanged(getWidth(), getHeight(), 0, 0);
                                if (mIsMovingThumb) {
                                    setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
                                    onSizeChanged(getWidth(), getHeight(), 0, 0);
                                handled = true;
                                }
                                 break;
            case MotionEvent.ACTION_CANCEL:
                                getParent().requestDisallowInterceptTouchEvent(false);
                                mIsMovingThumb = false;
                             handled = true;
                                 break;

        }
        return true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM