在Android中,我们如何检测用户是否触摸了按钮并将其拖出了该按钮的区域?
检查MotionEvent。MOVE_OUTSIDE:查看MotionEvent.MOVE:
private Rect rect; // Variable rect to hold the bounds of the view
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
// Construct a rect of the view's bounds
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
}
if(event.getAction() == MotionEvent.ACTION_MOVE){
if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){
// User moved outside bounds
}
}
return false;
}
注意:如果你想以Android 4.0为目标,一个充满新可能性的世界将打开:http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER
Entreco发布的答案需要对我的情况进行一些微调。我不得不替换:
if(!rect.contains((int)event.getX(), (int)event.getY()))
对于
if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY()))
因为event.getX()
和event.getY()
仅适用于ImageView本身,而不适用于整个屏幕。
我遇到了与OP相同的问题,我想知道何时(1)特定的View
被按下,以及(2a)何时在View
上释放按下触摸或(2b)何时按下触摸移动到View
的边界之外。我把这个线程中的各种答案组合在一起,创建了一个简单的View.OnTouchListener
扩展(名为SimpleTouchListener
),这样其他人就不必摆弄MotionEvent
对象了。这个类的来源可以在这里或这个答案的底部找到。
要使用这个类,只需将其设置为View.setOnTouchListener(View.OnTouchListener)
方法的参数,如下所示:
myView.setOnTouchListener(new SimpleTouchListener() {
@Override
public void onDownTouchAction() {
// do something when the View is touched down
}
@Override
public void onUpTouchAction() {
// do something when the down touch is released on the View
}
@Override
public void onCancelTouchAction() {
// do something when the down touch is canceled
// (e.g. because the down touch moved outside the bounds of the View
}
});
以下是欢迎您添加到项目中的类的来源:
public abstract class SimpleTouchListener implements View.OnTouchListener {
/**
* Flag determining whether the down touch has stayed with the bounds of the view.
*/
private boolean touchStayedWithinViewBounds;
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchStayedWithinViewBounds = true;
onDownTouchAction();
return true;
case MotionEvent.ACTION_UP:
if (touchStayedWithinViewBounds) {
onUpTouchAction();
}
return true;
case MotionEvent.ACTION_MOVE:
if (touchStayedWithinViewBounds
&& !isMotionEventInsideView(view, event)) {
onCancelTouchAction();
touchStayedWithinViewBounds = false;
}
return true;
case MotionEvent.ACTION_CANCEL:
onCancelTouchAction();
return true;
default:
return false;
}
}
/**
* Method which is called when the {@link View} is touched down.
*/
public abstract void onDownTouchAction();
/**
* Method which is called when the down touch is released on the {@link View}.
*/
public abstract void onUpTouchAction();
/**
* Method which is called when the down touch is canceled,
* e.g. because the down touch moved outside the bounds of the {@link View}.
*/
public abstract void onCancelTouchAction();
/**
* Determines whether the provided {@link MotionEvent} represents a touch event
* that occurred within the bounds of the provided {@link View}.
*
* @param view the {@link View} to which the {@link MotionEvent} has been dispatched.
* @param event the {@link MotionEvent} of interest.
* @return true iff the provided {@link MotionEvent} represents a touch event
* that occurred within the bounds of the provided {@link View}.
*/
private boolean isMotionEventInsideView(View view, MotionEvent event) {
Rect viewRect = new Rect(
view.getLeft(),
view.getTop(),
view.getRight(),
view.getBottom()
);
return viewRect.contains(
view.getLeft() + (int) event.getX(),
view.getTop() + (int) event.getY()
);
}
}
我在OnTouch中添加了一些日志记录,发现MotionEvent.ACTION_CANCEL
被击中。这对我来说已经足够了…
前两个答案都很好,除非视图在滚动视图中:当因为移动手指而进行滚动时,它仍然注册为触摸事件,但不是MotionEvent.ACTION_move事件。因此,为了改进答案(只有当您的视图位于滚动元素内时才需要):
private Rect rect; // Variable rect to hold the bounds of the view
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
// Construct a rect of the view's bounds
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
} else if(rect != null && !rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){
// User moved outside bounds
}
return false;
}
我在Android 4.3和Android 4.4 上测试了这个
我没有注意到莫里茨的答案和前2名之间有任何区别,但这也适用于他的答案:
private Rect rect; // Variable rect to hold the bounds of the view
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
// Construct a rect of the view's bounds
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
} else if (rect != null){
v.getHitRect(rect);
if(rect.contains(
Math.round(v.getX() + event.getX()),
Math.round(v.getY() + event.getY()))) {
// inside
} else {
// outside
}
}
return false;
}
Reusable-Kotlin解决方案
我从两个自定义扩展功能开始:
val MotionEvent.up get() = action == MotionEvent.ACTION_UP
fun MotionEvent.isIn(view: View): Boolean {
val rect = Rect(view.left, view.top, view.right, view.bottom)
return rect.contains((view.left + x).toInt(), (view.top + y).toInt())
}
然后倾听对视图的触摸。只有ACTION_DOWN最初出现在视图中时,才会触发此项。当你松开手指时,它会检查它是否仍在视图中。
myView.setOnTouchListener { view, motionEvent ->
if (motionEvent.up && !motionEvent.isIn(view)) {
// Talk your action here
}
false
}
view.setClickable(true);
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (!v.isPressed()) {
Log.e("onTouch", "Moved outside view!");
}
return false;
}
});
CCD_ 11使用CCD_。如果您不需要slop,只需从内部view.pointInView
复制逻辑(它是公共的,但是隐藏的,因此它不是官方API的一部分,随时可能消失)。
view.setClickable(true);
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.setTag(true);
} else {
boolean pointInView = event.getX() >= 0 && event.getY() >= 0
&& event.getX() < (getRight() - getLeft())
&& event.getY() < (getBottom() - getTop());
boolean eventInView = ((boolean) v.getTag()) && pointInView;
Log.e("onTouch", String.format("Dragging currently in view? %b", pointInView));
Log.e("onTouch", String.format("Dragging always in view? %b", eventInView));
v.setTag(eventInView);
}
return false;
}
});
虽然@FrostRocket的答案是正确的,但您也应该使用view.getX()和Y来解释翻译的变化:
view.getHitRect(viewRect);
if(viewRect.contains(
Math.round(view.getX() + event.getX()),
Math.round(view.getY() + event.getY()))) {
// inside
} else {
// outside
}
这里有一个View.OnTouchListener
,您可以使用它来查看用户的手指是否在视图之外时发送了MotionEvent.ACTION_UP
:
private OnTouchListener mOnTouchListener = new View.OnTouchListener() {
private Rect rect;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v == null) return true;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
return true;
case MotionEvent.ACTION_UP:
if (rect != null
&& !rect.contains(v.getLeft() + (int) event.getX(),
v.getTop() + (int) event.getY())) {
// The motion event was outside of the view, handle this as a non-click event
return true;
}
// The view was clicked.
// TODO: do stuff
return true;
default:
return true;
}
}
};
如果拖动到视图外,则调用"ACTION_CANCEL"事件。因此需要禁止父视图拦截触摸事件:
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
(parent as? ViewGroup?)?.requestDisallowInterceptTouchEvent(true)
}
MotionEvent.ACTION_UP -> {
(parent as? ViewGroup?)?.requestDisallowInterceptTouchEvent(false)
}
else -> {
}
}
return true
}
然后你可以检查触摸点是否在你的视野之外!