Android 用两只拇指操作SeekBar

Android 用两只拇指操作SeekBar,android,android-layout,seekbar,Android,Android Layout,Seekbar,我曾经用两个拇指来实现SeekBar,我得到了输出 但该搜索栏有一个问题,即它允许拇指1和拇指2交叉,即它允许拇指1的值大于拇指2的值,根据我的要求,这是不允许的 为了实现这一点,应在以下代码中进行哪些修改 public class SeekBarWithTwoThumb extends ImageView { private String TAG = this.getClass().getSimpleName(); private Bitmap thumb = BitmapF

我曾经用两个拇指来实现SeekBar,我得到了输出

但该搜索栏有一个问题,即它允许拇指1拇指2交叉,即它允许拇指1的值大于拇指2的值,根据我的要求,这是不允许的

为了实现这一点,应在以下代码中进行哪些修改

public class SeekBarWithTwoThumb extends ImageView {

    private String TAG = this.getClass().getSimpleName();
    private Bitmap thumb = BitmapFactory.decodeResource(getResources(),
            R.drawable.leftthumb);
    private int thumb1X, thumb2X;
    private int thumb1Value, thumb2Value;
    private int thumbY;
    private Paint paint = new Paint();
    private int selectedThumb;
    private int thumbHalfWidth;
    private SeekBarChangeListener scl;


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

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (getHeight() > 0)
            init();
    }

    private void init() {
        printLog("View Height =" + getHeight() + "\t\t Thumb Height :"
                + thumb.getHeight());
        if (thumb.getHeight() > getHeight())
            getLayoutParams().height = thumb.getHeight();

        thumbY = (getHeight() / 2) - (thumb.getHeight() / 2);
        printLog("View Height =" + getHeight() + "\t\t Thumb Height :"
                + thumb.getHeight() + "\t\t" + thumbY);

        thumbHalfWidth = thumb.getWidth()/2;
        thumb1X = thumbHalfWidth;
        thumb2X = getWidth()/2 ;
        invalidate();
    }
    public void setSeekBarChangeListener(SeekBarChangeListener scl){
        this.scl = scl;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(thumb, thumb1X - thumbHalfWidth, thumbY,
                paint);
        canvas.drawBitmap(thumb, thumb2X - thumbHalfWidth, thumbY,
                paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int mx = (int) event.getX();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (mx >= thumb1X - thumbHalfWidth
                    && mx <= thumb1X + thumbHalfWidth) {
                selectedThumb = 1;
                printLog("Select Thumb 1");
            } else if (mx >= thumb2X - thumbHalfWidth
                    && mx <= thumb2X + thumbHalfWidth) {
                selectedThumb = 2;
                printLog("Select Thumb 2");
            }
            break;
        case MotionEvent.ACTION_MOVE:
            printLog("Mouse Move : " + selectedThumb);

            if (selectedThumb == 1) {
                thumb1X = mx;
                printLog("Move Thumb 1");
            } else if (selectedThumb == 2) {
                thumb2X = mx;
                printLog("Move Thumb 2");
            }
            break;
        case MotionEvent.ACTION_UP:
            selectedThumb = 0;
            break;
        }

        if(thumb1X < 0)
            thumb1X = 0;

        if(thumb2X < 0)
            thumb2X = 0;

        if(thumb1X > getWidth() )
            thumb1X =getWidth() ;

        if(thumb2X > getWidth() )
            thumb2X =getWidth() ;

        invalidate();
        if(scl !=null){
            calculateThumbValue();
            scl.SeekBarValueChanged(thumb1Value,thumb2Value);
        }
        return true;
    }

    private void calculateThumbValue(){
        thumb1Value = (100*(thumb1X))/(getWidth());
        thumb2Value = (100*(thumb2X))/(getWidth());
    }
    private void printLog(String log){
        Log.i(TAG, log);
    }

    interface SeekBarChangeListener{
        void SeekBarValueChanged(int Thumb1Value,int Thumb2Value);
    }


}
public类seekbarwithtwoothumb扩展图像视图{
私有字符串标记=this.getClass().getSimpleName();
私有位图thumb=BitmapFactory.decodeResource(getResources(),
左拇指);
私有int thumb1X,thumb2X;
私有int thumb1Value,thumb2Value;
私人拇指;
私人油漆=新油漆();
私有int-selectedThumb;
半宽度;
私人seekbarchanger;
public SeekBarWithTwoThumb(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
}
public SeekBarWithTwoThumb(上下文上下文、属性集属性){
超级(上下文,attrs);
}
public SeekBarWithTwoThumb(上下文){
超级(上下文);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
超级测量(宽度测量、高度测量);
如果(getHeight()>0)
init();
}
私有void init(){
打印日志(“查看高度=“+getHeight()+”\t\t拇指高度:”
+thumb.getHeight());
if(thumb.getHeight()>getHeight())
getLayoutParams().height=thumb.getHeight();
thumbY=(getHeight()/2)-(thumb.getHeight()/2);
打印日志(“查看高度=“+getHeight()+”\t\t拇指高度:”
+thumb.getHeight()+“\t\t”+thumbY);
thumbHalfWidth=thumb.getWidth()/2;
拇指1X=拇指半宽;
thumb2X=getWidth()/2;
使无效();
}
公共无效设置seeKBarchaneListener(seeKBarchaneListener scl){
这是scl=scl;
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
canvas.drawBitmap(thumb,thumb1X-thumbHalfWidth,thumbY,
油漆);
canvas.drawBitmap(thumb,thumb2X-thumbHalfWidth,thumbY,
油漆);
}
@凌驾
公共布尔onTouchEvent(运动事件){
intmx=(int)event.getX();
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
如果(mx>=thumb1X-thumbHalfWidth
&&mx=拇指2X-拇指半宽
&&mx getWidth())
thumblex=getWidth();
如果(thumb2X>getWidth())
thumb2X=getWidth();
使无效();
如果(scl!=null){
calculateThumbValue();
scl.SeekBarValueChanged(thumb1Value,thumb2Value);
}
返回true;
}
私有void calculateThumbValue(){
thumb1Value=(100*(thumb1X))/(getWidth());
thumb2Value=(100*(thumb2X))/(getWidth());
}
私有void打印日志(字符串日志){
Log.i(标签、日志);
}
接口SeeKBarChangListener{
无效SeekBarValueChanged(int-Thumb1Value,int-Thumb2Value);
}
}

感谢您的帮助。

在本次活动的
OnTouch
结束时,请将您的代码替换为以下代码。希望对您有所帮助

    calculateThumbValue();

    if(thumb1Value < thumb2Value) {
        invalidate();
        if(scl != null){
            scl.SeekBarValueChanged(thumb1Value, thumb2Value);
        }
    }
calculateThumbValue();
如果(thumb1Value
在Touch事件的
结束时,用下面的代码替换您的代码。希望对您有所帮助

    calculateThumbValue();

    if(thumb1Value < thumb2Value) {
        invalidate();
        if(scl != null){
            scl.SeekBarValueChanged(thumb1Value, thumb2Value);
        }
    }
calculateThumbValue();
如果(thumb1Value
它正在工作,但应用程序在几秒钟后进入ANR模式…知道为什么吗?不…我不知道它的行为是这样的。因为thumb是位图,我认为它的内存不足错误它正在工作,但应用程序在几秒钟后进入ANR模式…知道为什么吗?不…我不知道它的行为是这样的。因为thumb是位图,我认为这是内存不足的错误