Dragging across an android button
I would like an android button to be triggered if a user drags across it and releases his finger on it in addition to the standard activation if it is clicked and released.
I was trying to use the ACTION_MOVE MotionEvent, but this event is only triggered between ACTION_DOWN and ACTION_UP events. I also think that ACTION_OUTSIDE might be a solution. Is there a way to do this?
Here is my code with the actions taken stripped out (b is a button):
b.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()== MotionEvent.ACTION_DOWN){
//Tiggers correctly
开发者_JS百科 } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
//Triggers correctly between events, but not when the finger is dragged ontop outside of the element
} else if (event.getAction() == MotionEvent.ACTION_OUTSIDE){
//Can't get this to trigger in any case but I think it might be the solution
} else if (event.getAction() == MotionEvent.ACTION_UP){
//Triggers correctly
}
}
return false;
}
});
ACTION_OUTSIDE can be triggered only when your touch area is extended by the ViewRoot.
If you think about dragging, you have 2 options, but for both of them you can detect rectangle, where your button positioned on screen View.getHitRect(Rect) and then, basing on this knowledge you can decide wheter MotionEvent is inside Rect or not.
Otherwise you can create DragLayer and implement all logic there.
You might want to give the Gesture Listener a try. There is a rather good description about how to use it here.
It seems like you need to extend Button and add possibilities for gesture detection in order to accomplish this.
精彩评论