Android Touch Events Pointer Down - android

Hi I am trying to detect when 2 fingers are touching the screen:
case MotionEvent.ACTION_POINTER_2_DOWN: {
twoFing=true;
return true;
}
the problem is that:
public static final int ACTION_POINTER_2_DOWN
is depreceted, the doc says:
*Constant for getActionMasked(): A non-primary pointer has gone down.
Use getActionIndex() to retrieve the index of the pointer that changed.
The index is encoded in the ACTION_POINTER_INDEX_MASK bits of the unmasked action returned by getAction().*
but I don't understand how to use it... How could I detect that there are 2 pointers? ActionUP and DOwn always say there is only one pointer if I try getPointerIndex()
thanks a lot
EDIT: I post here the full code to be more clear about the problem. My code is working BUT as the ACTION_POINTER_2_DOWN is a deprecated value I want to replace it by something else but I don't know how.
#SuppressWarnings("deprecation")
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN : {
Log.i(TAG, "Action Down");
downX = event.getX(0);
downY = event.getY(0);
return true;
}
case MotionEvent.ACTION_UP: {
upX = event.getX(0);
upY = event.getY(0);
float deltaX = downX - upX;
float deltaY = downY - upY;
Log.i(TAG, "Action UP deltaX="+deltaX+", deltaY="+deltaY);
// swipe vertical?
if(Math.abs(deltaY) > MIN_DISTANCE && twoFing){
twoFing=false;
// top or down
if(deltaY < 0 )
{
if(slide.zoom==1)
slide.zoom=0;
Log.i(TAG, "Going Down zooming in");
//return true;
}
if((deltaY > 0) )
{
if(slide.zoom==0)
slide.zoom=1;
Log.i(TAG, "Going up zoomig out");
//return true;
}
return true;
}
// swipe horizontal?
if(Math.abs(deltaX) > MIN_DISTANCE && !twoFing){
// left or right
if(deltaX < 0) { this.slideToTheLeft(); return true; }
if(deltaX > 0) { this.slideToTheRight(); return true; }
return true;
}
return false;
}
case MotionEvent.ACTION_POINTER_2_DOWN: {
twoFing=true; //inform that the touch was made with 2 fingers
Log.i(TAG, "Action Second pointer down");
return true;
}
}
return false;
}

I'm working on pointer stuff myself right now. You want to switch as follows
switch(event.getAction() & MotionEvent.ACTION_MASK);
case MotionEvent.ACTION_POINTER_DOWN:
ACTION_DOWN is about the first finger down.
ACTION_POINTER_DOWN is about the second finger.
See http://www.vogella.com/articles/AndroidTouch/article.html for a fairly clear description.

MotionEvent.ACTION_DOWN is only for the first pointer. GetActionMasked() will return MotionEvent.ACTION_POINTER_DOWN for subsequent pointers.

Did you try with getPointerId(index) with different values for index ?
See the difference between Id and Index:
Index vs. ID
At a higher level, touchscreen data from a snapshot in time may not be
immediately useful since touch gestures involve motion over time
spanning many motion events. A pointer index does not necessarily
match up across complex events, it only indicates the data’s position
within the MotionEvent. However this is not work that your app has to
do itself. Each pointer also has an ID mapping that stays persistent
across touch events. You can retrieve this ID for each pointer using
MotionEvent.getPointerId(index) and find an index for a pointer ID
using MotionEvent.findPointerIndex(id).
The best solution should be to save the state of all the pointers and update/compare them you receive a new TouchEvent.
Check this example.

Related

How to have both pinch zoom and click feature in one layout?

I have a layout that need to have pinch zoom feature and inside of that layout there is also many child(imageview) that will have a click feature. The click feature will open another activity.
I have use this code to create a custom layout https://gist.github.com/klarson2/4f737adf7f2577dc0fd09efb85eff3b1
But the problem is when first finger touch the child, then second finger touch anywhere it will not zoom.
Then I put in this code inside the zoom code (https://gist.github.com/klarson2/4f737adf7f2577dc0fd09efb85eff3b1):
#Override
public boolean onInterceptTouchEvent(MotionEvent ev){
switch (ev.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
startClickTime = Calendar.getInstance().getTimeInMillis(); //start time when first finger land
startX = ev.getX() - prevDx;
startY = ev.getY() - prevDy;
break; //return false will go to frag
case MotionEvent.ACTION_POINTER_DOWN:
clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if(clickDuration > MAX_CLICK_DURATION){
// letting go from drag or zooming
Log.i("Zoom1", " action pointer down test");
return true; // remain at zoomlayout and do ontouch
}
break;
case MotionEvent.ACTION_UP:
clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
Log.i("Zoom1", " action up test");
if(clickDuration < MAX_CLICK_DURATION){
return false;
}
else {
// letting go from drag or zooming
return true;
}
case MotionEvent.ACTION_MOVE:
clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if(clickDuration >= MAX_CLICK_DURATION){
// to long on move, definitely is drag
mode = Mode.DRAG;
return true;
}
else {
return false;
}
}
return false;
}
Then another problem come out is that both finger touch the child and pinch zoom. The zoom offset.
this is how it show like
This is the idea of how the app will run. But will it work? I have try many ways but in the end there is a lot of error. Can anyone help me out. Thank you.

Consume only two finger touch

I want to catch ONLY two fingers touch (one finger touch must be NOT consumed).
Unfortunately, I need to consume the "one finger touch" in order to get the "two finger touch" events.
I hope I'm a clear.
Here my code :
#Override
public boolean onTouch(View v, MotionEvent m)
{
boolean consumed = false;
int pointerCount = m.getPointerCount();
int action = m.getActionMasked();
if(pointerCount == 2)
{
switch (action)
{
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
consumed = true;
break;
case MotionEvent.ACTION_UP:
// Work
break;
case MotionEvent.ACTION_MOVE:
// Some stuff
break;
default:
break;
}
}
else if (pointerCount == 1 && action == MotionEvent.ACTION_DOWN)
{
// This is needed to get the 2 fingers touch events...
consumed = true;
}
return consumed;
}
I've ran into a similar problem and solved my problem by handling all touches at my top view, and propagating the appropriate actions to the views below. In my example, I had an overlay layer that needed to intercept scale gestures (a recognizer is attached in my constructor), while still propagating single touches to my map view below. I haven't sent the actual touch event to my map view, but I've handled it in my top view and sent the appropriate "panning" action to my view below.
#Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getPointerCount() == 2) {
if(event.getActionMasked() == MotionEvent.ACTION_DOWN){
CameraUpdate update = CameraUpdateFactory.newLatLng(initialCoords);
//attachedMapFragment.getMap().moveCamera(update);
}
scaleGestureDetector.onTouchEvent(event);
}else{
if(attachedMapFragment != null) {
if(event.getActionMasked() == MotionEvent.ACTION_DOWN){
initialCoords = attachedMapFragment.getMap().getCameraPosition().target;
lastTouchX = event.getX();
lastTouchY = event.getY();
}else if(event.getActionMasked() == MotionEvent.ACTION_MOVE){
float deltaX = event.getX() - lastTouchX;
float deltaY = event.getY() - lastTouchY;
lastTouchX = event.getX();
lastTouchY = event.getY();
Projection projection = attachedMapFragment.getMap().getProjection();
Point center = projection.toScreenLocation(attachedMapFragment.getMap().getCameraPosition().target);
Point newCenter = new Point(center.x - (int)deltaX, center.y - (int)deltaY);
LatLng newCoords = projection.fromScreenLocation(newCenter);
CameraUpdate update = CameraUpdateFactory.newLatLng(newCoords);
attachedMapFragment.getMap().moveCamera(update);
}
}
}
return true;
}
This way, I had a scale recognizer on my custom view, while the map was still panning appropriately to single touch. Actually, map view wasn't getting any touches at all, my custom view was "routing" the action as seen above. While not always ideal, this would solve the problem in many cases.

How do you make an onTouchListener keep going as long as the view is being pressed?

I am using a small square RelativeLayout as a game pad for movement in a neighboring GLSurfaceView.
I set it up so that in the onTouch method of the RelativeLayout's onTouchListenner I call a method built into the GLSurfaceView that updates the translation and rotation coordinates for my drawing.
I have everything working fine, except for the fact that touch events are only triggered if the user moves his or her finger on the RelativeLayout.
I would like it to feel a little bit like a joystick: if you leave your finger pressed on the top of the RelativeLayout, then you will keep going "up" ( or more programatically: the event.getX() and event.getY() that were sent last should get looped, or re-sent, in the GLSurfaceView until the user either moves his finger inside the RelativeLayout, or stops touching the RelativeLayout altogether.)
What should I use to detect whether or not the RelativeLayout is currently being touched (even if there is no motion in the said touch)?
Thanks!
So this is what I came up pretty much simultaneously with csmcklvey
if(event.getAction()==MotionEvent.ACTION_UP)
gM.isTouchedL = false;
else
gM.isTouchedL = true;
return true;
Where .isTouchedL is the "control boolean" I use in the GLSurfaceView
Green lit csmc's answer anyways! :)
I have used rotation animation in one of my apps, which is using the onTouchEven of view. Major function has been performed in event "ACTION_MOVE". It will help you, to get through.
public boolean onTouch(View v, MotionEvent event)
{
final float xc = volumeButton.getWidth() / 2;
final float yc = volumeButton.getHeight() / 2;
final float x = event.getX();
final float y = event.getY();
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
// volumeButton.clearAnimation();
// mCurrAngle = Math.toDegrees(Math.atan2(x - xc, yc - y));
break;
}
case MotionEvent.ACTION_MOVE:
{
mPrevAngle = mCurrAngle;
mCurrAngle = Math.toDegrees(Math.atan2(x - xc, yc - y));
animate(mPrevAngle, mCurrAngle, 100);
break;
}
case MotionEvent.ACTION_UP :
{
mPrevAngle = mCurrAngle = 0;
break;
}
}
return true;
}
You might be able to use the event.getAction() method of the MotionEvent parameter of your onTouch() method. You can check to see if it is MotionEvent.ACTION_DOWN or MotionEvent.ACTION_UP.
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//start something
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
//stop something
}
My thought is that you might be able to start or stop a thread in this manner but either way this might at least give you some more information about when the user actually presses and lifts his/her finger.

Make OnTouchListener update all the time

I have an Android applciation which I want it to draw circles around
I used OnTouchListener.
The problem is, when the users "holds" the circle in place, it doesn't update the action.
How can I know if the user's finger is on,when he doesn't move it, with the OnTouchListener
You'll have to check if what kind of event is triggering onTouchEvent.
#Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Do Nothing
return true;
case MotionEvent.ACTION_MOVE:
//Do Something
path.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
//Do Nothing
break;
default:
return false;
}
// Schedules a repaint.
invalidate();
return true;
}
More Information about MotionEvents can be found here.
You can know that by using
MotionEvents:
Reed more here: MotionEvents
ACTION_DOWN is for the first finger that touches the screen. This starts the gesture. The pointer data for this finger is always at index 0 in the MotionEvent.
ACTION_POINTER_DOWN is for extra fingers that enter the screen beyond the first. The pointer data for this finger is at the index returned by getActionIndex().
ACTION_POINTER_UP is sent when a finger leaves the screen but at least one finger is still touching it. The last data sample about the finger that went up is at the index returned by getActionIndex().
ACTION_UP is sent when the last finger leaves the screen. The last data sample about the finger that went up is at index 0. This ends the gesture.
ACTION_CANCEL means the entire gesture was aborted for some reason. This ends the gesture.
here is a good answer to read StackOverFlow
Filter touches by what kind they are:
boolean onScreen = false;
#Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX(); //X coord of the touch
float eventY = event.getY(); //Y coord of the touch
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// User has touched the screen
onScreen = true;
return true;
case MotionEvent.ACTION_MOVE:
// Finger is being dragged on the screen
onScreen = true; //Not required, just for clarity purposes.
break;
case MotionEvent.ACTION_UP:
// User has lifter finger
onScreen = false;
break;
default:
return false;
}
if(onScreen) {
//Finger is on the screen
}
return true;
}

Implement both swipe and longclick on list item?

I need to implement both Longclick and Left & right swipe on a list view and get the listitem on which the action was performed. This method seemed really promising.
Problems:
1.ACTION_MOVE is fired only once at the start so the diff is really minimal
2.If i use a default in the switch i get the last location but onClick or onLongClick is never fired. Here is what i tried.. Is it possible to fire a fake ACTION to cause itemClick/itemlongclick to execute.
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
downX = event.getX();
downY = event.getY();
mSwipeDetected = Action.NONE;
Log.i("MyTags","Down Event");
Log.i("MyTags",String.valueOf(downX)+","+String.valueOf(downY));
return false; // allow other events like Click to be processed
case MotionEvent.ACTION_MOVE:
upX = event.getX();
upY = event.getY();
Log.i("MyTags","Move Event");
Log.i("MyTags",String.valueOf(upX)+","+String.valueOf(upY));
moveEnabled=true;
return false;
case MotionEvent.ACTION_UP:
upX = event.getX();
upY = event.getY();
Log.i("MyTags","UP Event");
Log.i("MyTags",String.valueOf(upX)+","+String.valueOf(upY));
return false;
default:
upX = event.getX();
upY = event.getY();
Log.i("MyTags","Default Event");
Log.i("MyTags",String.valueOf(upX)+","+String.valueOf(upY));
if(moveEnabled)
{
diffX=downX-upX;
diffY=downY-upY;
abs_X=Math.abs(diffX);
abs_Y=Math.abs(diffY);
moveEnabled=false;
if((abs_X>abs_Y)&(abs_X>MINIMUM_X))
{
if(diffX>0)
{
mSwipeDetected=Action.LEFT;
Log.i("MyTags","Left Swipe");
event.setAction(MotionEvent.ACTION_UP);
return false;
}
else if(diffX<0)
{
mSwipeDetected=Action.RIGHT;
Log.i("MyTags","Right Swipe");
event.setAction(MotionEvent.ACTION_UP);
return false;
}
}
}
return false;
}
}
I kind of used a hack to fix my issue.What happens is since the events are consumed, i never get the result in the onitemclick event. What i have done is to fire my own Down and Up events once a swipe action is detected and during those fake events i return false so that its passed down to the itemclick listener. We need to be a bit careful here as the event seems to read from the result 2 times , for this i turn the listener on and off , there by containing the output.
ACTION_MOVE:
//when swipe is detected fire a fake down event
event.setAction(MotionEvent.ACTION_DOWN);
v.dispatchTouchEvent(event);
ACTION_DOWN:
if(fake down event)
//set some flags & dispatch fake event
event.setAction(MotionEvent.ACTION_UP);
v.dispatchTouchEvent(event);
return false
else do regular handling
ACTION_UP:
if fake down event
dispatch fake up event
else ..
here 'v' is the view on which the event has to be fired , in my case its the listview.
Since we dont have it right-off it has to be passed in through the constructor of the swipeDetector class. As follows..
ListView v;
SwipeDetector(ListView lv)
{
this.v=lv;
}

Categories

Resources