Reputation: 53
i have a SurfaceView and i want to recognize the MotionEvent Action_Up, but only the Action_Down event is triggered. Also i want to recognize another touch, while the first finger is still on the screen, but the OnTouch Event isnt triggered again.
@Override
public boolean onTouch(View arg0, MotionEvent event) {
int pointerCount = event.getPointerCount();
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
//blabla
} else /* if (event.getAction() == android.view.MotionEvent.ACTION_UP) */{
//blabla
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int pointerCount = event.getPointerCount();
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
//blabla
} else /* if (event.getAction() == android.view.MotionEvent.ACTION_UP) */{
//blabla
}
return super.onTouchEvent(event);
}
The pointerCount is always 1, no matter how many fingers are on the screen.
Upvotes: 2
Views: 1378
Reputation: 2108
Try capturing the getAction in a variable instead of calling it multiple times in your onTouch. Also make sure you're letting the listener know when the action is consumed.
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction();
boolean consumed = false;
switch(action){
case MotionEvent.ACTION_DOWN:
//down stuff logic
consumed = true;
break;
case MotionEvent.ACTION_UP:
//up stuff logic
consumed = true;
break;
case MotionEvent.ACTION_MOVE:
//move stuff logic
consumed = true;
break;
}
return consumed;
}
As for number of fingers on the screen, remember multitouch support started at API level 5, so depending on your phone/minSDK you might not have it.
Upvotes: 0
Reputation: 63293
You must return true for an ACTION_DOWN
event in your OnTouchListener
if you are interested in being notified of the remaining events associated with that touch (ACTION_MOVE
, ACTION_UP
, etc.)
I would also recommend EITHER monitoring the touches using a listener OR in the onTouchEvent
method of the view...not both.
HTH
Upvotes: 1