gpt4 book ai didi

android - 用两个拇指使用 SeekBar

转载 作者:行者123 更新时间:2023-11-30 03:59:05 27 4
gpt4 key购买 nike

我用过 this link用两个拇指实现SeekBar,我得到了输出。

但是这个搜索栏有一个问题,它允许 thumb 1thumb 2 的交叉,即它允许 thumb 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);
}


}

感谢任何帮助。

最佳答案

OnTouch 事件结束时,将您的代码替换为以下代码。希望对你有帮助

    calculateThumbValue();

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

关于android - 用两个拇指使用 SeekBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12836904/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com