Reputation: 8562
Please have a look at this code sample taken from overridden onTouchEvent
of a View
,
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//code move pointer position
break;
case MotionEvent.ACTION_MOVE:
// code to draw line from x to y
break;
case MotionEvent.ACTION_UP:
//Release pointer
break;
default:
return false;
}
}
This draws line fine, But the issue is when I touch one corner with the single finger and touch next corner of the screen with another finger and hold it then release the first finger. It draws line automatically from one finger to another. But in my app, it should be mandatory for the user to draw line manually by using a single finger.
Here is whats happening
To overcome this issue I tried as far as I could. It's not either a Multitouch
event.
Is this fault of the Android framework?
In addition:
I downloaded many drawing apps from Playstore to check if they've prevented such auto drawing. But all the apps behave same as my one.
How can I resolve this?
Upvotes: 1
Views: 958
Reputation: 78
you just add case MotionEvent.ACTION_POINTER_UP
and release your points.
when your case in case MotionEvent.ACTION_POINTER_UP
,at this time, you up your finger,the program will run in action_move,at this time ,your just break off this action.
my example like this:
boolean isRelease = false;
@Override public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isRelease = false;
//code move pointer position
break;
case MotionEvent.ACTION_MOVE:
if (isRelease) {
return true;
}
// code to draw line from x to y
break;
case MotionEvent.ACTION_POINTER_UP:
isRelease = true;
//Release pointer
break;
case MotionEvent.ACTION_UP:
isRelease = true;
//Release pointer
break;
}
return true;
}
sorry for my poor English.
Upvotes: 1
Reputation: 93708
Despite what you say, you are running into multitouch events. In Android, the initial touch down is an ACTION_DOWN. A second touch is an ACTION_POINTER_DOWN. Any moves after that, even a single pixel, by either finger will cause an ACTION_MOVE. So you more or less get constant moves. The solution is to use the pointer ids to figure out which finger was the first finger down, and only look at that pointer id when you get a MOVE, ignoring all the other pointers in it.
Upvotes: 1