Can someone walk me through the best way to do this? I want to make an interface that extends OnTouchListener
. Instead of one meathod called开发者_如何学C onTouch(View view, MotionEvent e)
i want 3 meathods; onPress()
onMove()
and onRelease()
. The onPress
meathod gets called when you press on the screen and the onMove
gets called when you move your finger across the screen. onRelease
gets called when you release your finger. All relevant answers are welcome.
You'd use the YourTouchListener
provided by duffymo by extending your View
class and adding a setYourTouchListener(YourTouchListener listener)
method to this class.
You'd then override onTouchEvent(MotionEvent event)
and call the relevant methods of your listener. Like so:
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
listener.onPress(this, event);
break;
case MotionEvent.ACTION_MOVE:
listener.onMove(this, event);
break;
case MotionEvent.ACTION_UP:
listener.onRelease(this, event);
break;
}
//this means that you have "used" this event. The ViewGroup will direct all further associated events straight here.
return true;
}
I would probably do essentially what CaspNZ posted, the only difference being that you shouldn't extend OnTouchListener
in this case. Implementing an interface means that you must give an implementation for all of its methods, so in this case onTouch
in addition to the three that you are creating, which is redundant. And of course if you still end up needing onTouch
for something, you can always implement OnTouchListener
in addition to YourTouchListener
.
You can use GestureListener
and use onFling(...)
method, which gives you possibility (MotionEvent e1
and MotionEvent e2
) to measure initial touch (on press) and final touch (release) and based on this you do your job. Also provides velocity
of MotionEvent
along x and y axis, measure pressure applied on the screen, etc. This will cut your time on writing the whole interface you are about to do.
精彩评论