Seekbar 仅在拖动 Thumb 时更改进度
Seekbar change progress ONLY when dragging Thumb
当用户 ONLY 触摸 Thumb
[= 时,我遇到了如何控制 Seekbar 进度的问题16=]
我知道这是一个重复的问题,但这里的链接让我对它有所了解 this and this。
经过几轮测试,下面是我的工作逻辑结果
mySeekbar.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Rect rcThumb = mySeekbar.getThumb().getBounds();
int iWidth = rcThumb.width();
if (event.getX() >= (rcThumb.left - iWidth) && event.getX() <= (rcThumb.right + iWidth)) {
return false;
} else
return true;
}
});
预期结果
无需拖动或点击
在拖动或点击期间
问题
您可以直接复制粘贴上面的代码进行测试。正如我们所知,当触摸或拖动时拇指会变大,但有一个问题是拇指在不触摸时会变大。这个问题不会经常发生,但很少发生。无论如何要解决这个问题?非常感谢您的帮助。
问题结果
无需拖动或点击
在拖动或点击期间
经过几轮测试,我终于完成了这个结果。干杯:)
private boolean mIsDragging;
private boolean isWithinThumb(MotionEvent event, SeekBar seekBar) {
Rect rcThumb = seekBar.getThumb().getBounds();
Rect rcDetectTouchArea = new Rect();
int iWidth = rcThumb.width();
int iHeight = rcThumb.height();
rcDetectTouchArea.left = rcThumb.left - iWidth;
rcDetectTouchArea.right = rcThumb.right + iWidth;
rcDetectTouchArea.bottom = rcThumb.bottom + iHeight;
Log.i("TAG", "rcDetectSize.left = " + rcDetectTouchArea.left + " | rcDetectSize.right = " + rcDetectTouchArea.right + " | rcDetectSize.bottom = " + rcDetectTouchArea.bottom + " | event.getX()= " + event.getX() + " | event.getY()= " + event.getY() + " | rcThumb area = " + rcDetectTouchArea.contains((int) event.getX(), (int) event.getY()));
return rcDetectTouchArea.contains((int) event.getX(), (int) event.getY());
}
mSbTest.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (isWithinThumb(event, mSbTest)) {
mIsDragging = true;
return false;
}
case MotionEvent.ACTION_UP:
if (mIsDragging) {
mIsDragging = false;
return false;
}
if (isWithinThumb(event, mSbTest)) {
return false;
} else {
return true;
}
case MotionEvent.ACTION_MOVE:
if (!mIsDragging) {
return true;
}
}
return onTouchEvent(event);
}
});
当用户 ONLY 触摸 Thumb
[= 时,我遇到了如何控制 Seekbar 进度的问题16=]
我知道这是一个重复的问题,但这里的链接让我对它有所了解 this and this。
经过几轮测试,下面是我的工作逻辑结果
mySeekbar.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Rect rcThumb = mySeekbar.getThumb().getBounds();
int iWidth = rcThumb.width();
if (event.getX() >= (rcThumb.left - iWidth) && event.getX() <= (rcThumb.right + iWidth)) {
return false;
} else
return true;
}
});
预期结果
无需拖动或点击
在拖动或点击期间
问题
您可以直接复制粘贴上面的代码进行测试。正如我们所知,当触摸或拖动时拇指会变大,但有一个问题是拇指在不触摸时会变大。这个问题不会经常发生,但很少发生。无论如何要解决这个问题?非常感谢您的帮助。
问题结果
无需拖动或点击
在拖动或点击期间
经过几轮测试,我终于完成了这个结果。干杯:)
private boolean mIsDragging;
private boolean isWithinThumb(MotionEvent event, SeekBar seekBar) {
Rect rcThumb = seekBar.getThumb().getBounds();
Rect rcDetectTouchArea = new Rect();
int iWidth = rcThumb.width();
int iHeight = rcThumb.height();
rcDetectTouchArea.left = rcThumb.left - iWidth;
rcDetectTouchArea.right = rcThumb.right + iWidth;
rcDetectTouchArea.bottom = rcThumb.bottom + iHeight;
Log.i("TAG", "rcDetectSize.left = " + rcDetectTouchArea.left + " | rcDetectSize.right = " + rcDetectTouchArea.right + " | rcDetectSize.bottom = " + rcDetectTouchArea.bottom + " | event.getX()= " + event.getX() + " | event.getY()= " + event.getY() + " | rcThumb area = " + rcDetectTouchArea.contains((int) event.getX(), (int) event.getY()));
return rcDetectTouchArea.contains((int) event.getX(), (int) event.getY());
}
mSbTest.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (isWithinThumb(event, mSbTest)) {
mIsDragging = true;
return false;
}
case MotionEvent.ACTION_UP:
if (mIsDragging) {
mIsDragging = false;
return false;
}
if (isWithinThumb(event, mSbTest)) {
return false;
} else {
return true;
}
case MotionEvent.ACTION_MOVE:
if (!mIsDragging) {
return true;
}
}
return onTouchEvent(event);
}
});