I am a newbie in Android, lately I got this weird thing:
I have my customized View, in onTouchEvent method, i tried to detect multitouch gesture, but the MotionEvent.ACTION_POINTER_DOWN will always be triggered no matter my second finger touches the view or not.
It seems like ACTION_POINTER_DOWN doesn't behaves the same as ACTION_DOWN, since ACTION_DOWN will not be triggered if my finger didn't touch the view.
Please tell me what's wrong with that, is that a bug?
Its working as intended. Once you touch down, all touch events go to your view until a cancel or an up occurs.
Related
I want to fire a method after a touch has been initiated and left the screen. I can detect the beginning of a touch by setting a OnTouch event to the View but can't detect when the hand leave the screen. I want to detect when does the touch stops. How can i detect this?
What you're looking for is http://developer.android.com/reference/android/view/MotionEvent.html - Specifically the use of MotionEvent.ACTION_UP. There is a lot of information on the link I've provided :)
A gesture starts with a motion event with ACTION_DOWN that provides the location of the first pointer down. As each additional pointer that goes down or up, the framework will generate a motion event with ACTION_POINTER_DOWN or ACTION_POINTER_UP accordingly. Pointer movements are described by motion events with ACTION_MOVE. Finally, a gesture end either when the final pointer goes up as represented by a motion event with ACTION_UP or when gesture is canceled with ACTION_CANCEL.
you can use MotionEvent.ACTION_UP to detect touch up.
I would look at this answer. Basically it says that you need to return true to process anything other than ACTION_DOWN.
I am overriding both onTouch and onClick. Each is supposed to result in different actions (very important). I don't want a touch to be both a touch and a click: it must be either-or. I am not having much luck. How do I get my app to discriminate between onTouch and onClick? Right now, either I get onTouch by itself or I get both onTouch and onClick together (based on whether I change onTouch to return true or false).
An easy solution is to just use onTouch(). Look for ACTION_UP and ACTION_DOWN. Store the position of the touch in ACTION_DOWN, and in ACTION_UP if the distance between the touch and the stored touch are < X then fire a custom click event, otherwise fire the drag event.
A specific method needs to be called whenever a gesture ends. Currently, I'm using an if statement to see if a MotionEvent is ACTION_UP to do this. However, I've discovered that if I lift my finger while swiping quickly, the MotionEvent is not an ACTION_UP, but an ACTION_MOVE. It only works properly if I lift while the finger is not moving.
Any suggestions or alternative solutions?
Thanks in advance.
EDIT: problem solved
Well, I figured out what was wrong. I was using some booleans to prevent ConcurrentModificationExceptions when onTouchEvent modifies a list of objects that my renderer was also drawing. Turns out that this was ending the method early half the time. I ended up synchronizing the lists, and now it works fine.
I'm trying to implement a drag-drop functionality for pre-honeycomb devices. I'd like to touch an item in one frame layout, drag it over another frame layout, and have the second frame see the ACTION_UP event. Unfortunately haven't found a way to do that, it seems only the view hierarchy that saw the original ACTION_DOWN gets notified of the ACTION_UP.
I've got the drag part working, but when I release my finger, the view underneath the ACTION_UP gets no notification at all. Is there any way to workaround this?
I ended up working around this by intercepting all of the events in the frame view with onInterceptTouchEvent
I want to track a finger touch on the screen. So what I did was to start recording the position when MotionEvent triggers ACTION_DOWN, but how do I know when the action is finished, at ACTION_CANCEL, or ACTION_UP?
What's the exact difference between them?
MotionEvent:
ACTION_UP: A pressed gesture has finished, the motion contains the final release location as well as any intermediate points since the last down or move event.
ACTION_CANCEL: The current gesture has been aborted.
ACTION_CANCEL occurs when the parent takes possession of the motion, for example when the user has dragged enough across a list view that it will start scrolling instead of letting you press the buttons inside of it. You can find out more about it at the viewgroup documentation: onInterceptTouchEvent.
so use ACTION_CANCEL when the action is dragged out of the parent, and ACTION_UP otherwise.
In general ACTION_UP is triggered by user when the guest is finished to definitely indicate that the user completed interacting with movement.
On the other hand ACTION_CANCEL is called by Android system to indicate that one of views took control using onInterceptTouchEvent() which returned true as a result system finished propagating the touch event.
Please take a look at a diagram