This is how you implement a touch listener in Android.
yourImageView.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
return false;
}
});
To detect double taps you use GestureDetector like this:
1) Make your own GestureDetector, derived from SimpleOnGestureListener and override the methods you're interested in (see google's docs on SimpleOnGestureListener for the exact methods you can override, I've done double tap here):
class MyGestureDetector extends GestureDetector.SimpleOnGestureListener
{
@Override
public boolean onDoubleTapEvent(MotionEvent e)
{
Log.i("Taghere..", "onDoubleTapEvent");
return true;
}
}
2) Create an instance of your gesture detector. I'm making a member variable and instantiating in onCreate.
private GestureDetector mDetector;
mDetector = new GestureDetector(this, new MyGestureDetector());
3) Setup a touch listener on your imageview and route the messages to your gesture detector:
ImageView iv = (ImageView)findViewById(R.id.yourimageviewid);
iv.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event)
{
mDetector.onTouchEvent(event);
return true;
}});
I would override the methods in MyGestureDetector and log to logcat like I'm doing on double tap to get a feel for how this works.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…