Reputation: 1784
I'm getting frustated with OnTouchEvent. I wanna detect only 5 fingers.How can I achieve that? Also the problem is that it calls multiple times. Here is my code :
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int pointerCount = event.getPointerCount();
System.out.println("My pointer....." + pointerCount);
final int action = event.getAction();
if(action == MotionEvent.ACTION_UP) {
if(pointerCount >= 4){
Log.d("MyActivity", "in onTouchEvent!");
Toast.makeText(MyclassActivity.this, "Finger !!"+pointerCount,Toast.LENGTH_SHORT).show();
Intent z = new Intent(MyclassActivity.this,
DashboardActivity.class);
startActivity(z);
finish();
}
}
return super.onTouchEvent(event);
}
I am not satisfied with this thing. let me take your help to get and count exact 5 fingers and avoid multiple times calling of onTouchevent.
Thanks,
Upvotes: 3
Views: 1043
Reputation: 741
i got lots of trouble knowing how does it work, so here is the basic things i came up with
public boolean onTouchEvent(MotionEvent event){
int eventaction = event.getAction();
String str= "";
//touch Events, i came up with the mask 5 by trial, hope it works for all devices
//eventaction == 0 match the first touch event ever
if( ( eventaction & 5 ) == 5 || eventaction == 0 ){
str= "Touch Event";
}
//Release Event, i came up with the mask 6 by trial, hope it works for all devices
//eventaction == 1 match the last release event ever, this makes it hard to know wich finger was removed
if( ( eventaction & 6 ) == 6 || eventaction == 1 ){
str= "Release Event:";
}
if( eventaction == 2 ){
str= "Move Event:";
return true;//it will make a mess in the logcat, if u want remove this line
}
str += " With Number Of fingers " + event.getPointerCount() ;
str += ", the finger triggered the event is : finger ";
//some stupid thing i have done, but it works
//these numbers was made based on the binary mask that i was able to figure out
//but it still has an issue with the last finger removed as its eventaction is always 0, but this can be pragmatically known by monitoring each finger touch and release
switch ( eventaction ){
case 0:
case 5:
case 6:
str += "1";
break;
case 261:
case 262:
str += "2";
break;
case 517:
case 518:
str += "3";
break;
case 773:
case 774:
str += "4";
break;
case 1029:
case 1030:
str += "5";
break;
case 1285:
case 1286:
str += "6";
break;
case 1541:
case 1542:
str += "7";
break;
case 1797:
case 1798:
str += "8";
break;
case 2053:
case 2054:
str += "9";
break;
case 2309:
case 2310:
str += "10";
break;
}
Log.d("Test", str );
return true;
}
Hope this helps anybody, if you still have missing info then i will be glad to help ^_^.
Upvotes: 1
Reputation: 4920
I believe MotionEvent.ACTION_POINTER_UP
is being called every time you remove a single finger from your screen. So if you touch your screen with 5 fingers it will turn true more than 1 time. Try using MotionEvent.ACTION_UP
in your implementation. Count all the fingers -> check when MotionEvent.ACTION_UP
is called -> do you code only if the highest amount of fingers was 5.
int maxPointercount;
int previousPointercount;
@Override
public boolean onTouch(View v, MotionEvent event) {
int currentpointerCount = event.getPointerCount();
Log.d("1", "My pointer = " + currentpointerCount); //what does it say here?
final int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
if(maxPointercount <= previousPointercount){
maxPointercount = currentpointerCount;
}
previousPointercount = currentpointerCount;
}
if(action == MotionEvent.ACTION_UP) {
Log.d("3", maxPointercount + " = maxPointercount");
if(maxPointercount == 5){ //or whatever amount of fingers, try it out.
//your code that will run 1 time
}
maxPointercount = 0;
previousPointercount = 0;
}
return super.onTouchEvent(event);
}
Edit: Fixed it again! Now it really works.
Upvotes: 2