Si está buscando un método como ImageView.setOnGestureListener, no hay ninguno. Puede ir a ver el código fuente de Android. Su mejor opción es manejarlo en onTouch() del objeto View.
Este es el GestureDetector más simple que puedo hacer.
Tengo una clase llamada GenesMotionDetector.java. Aquí está el código:
package gene.com.motioneventssample;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class GenesMotionDetector extends Activity implements GestureDetector.OnGestureListener {
private GestureDetector gestureScanner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nothing);
gestureScanner= new GestureDetector(getBaseContext(),this);
}
@Override
public boolean onTouchEvent(MotionEvent me) {
System.out.println("Inside onTouchEvent() of GenesMotionDetector.java");
return gestureScanner.onTouchEvent(me);
}
@Override
public boolean onDown(MotionEvent e) {
System.out.println("Inside onDown() of GenesMotionDetector.java");
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
System.out.println("Inside onFling() of GenesMotionDetector.java");
return true;
}
@Override
public void onLongPress(MotionEvent e) {
System.out.println("Inside onLongPress() of GenesMotionDetector.java");
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
System.out.println("Inside onScroll() of GenesMotionDetector.java");
return true;
}
@Override
public void onShowPress(MotionEvent e) {
System.out.println("Inside onShowPress() of GenesMotionDetector.java");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
System.out.println("Inside onSingleTapUp() of GenesMotionDetector.java");
return true;
}
}
El archivo de diseño XML correspondiente para esa clase es nothing.xml. Aquí está el código para ello:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/screen"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:background="#17528c"
android:text="testing"
android:layout_width="100dp"
android:layout_height="200dp" />
<ImageView
android:id="@+id/image"
android:background="#f8af20"
android:layout_width="100dp"
android:layout_height="200dp" />
</LinearLayout>
Ahora, tomando el GestureDetector más simple que puedo hacer (desde arriba) y modificándola por lo que quieres, me sale esto:
package gene.com.motioneventssample;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.widget.ImageView;
public class GenesMotionDetector3 extends Activity implements GestureDetector.OnGestureListener {
private GestureDetector gestureScanner;
ImageView mView3;
View.OnTouchListener gestureListener;
MotionEvent initialME, finalME;
private VelocityTracker mVelocityTracker = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nothing);
mView3 = (ImageView) findViewById(R.id.image);
// Gesture detection
gestureScanner = new GestureDetector(getBaseContext(), this);
gestureListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
initialME= event;
if(mVelocityTracker == null) {
// Retrieve a new VelocityTracker object to watch the velocity of a motion.
mVelocityTracker = VelocityTracker.obtain();
}
else {
// Reset the velocity tracker back to its initial state.
mVelocityTracker.clear();
}
// Add a user's movement to the tracker.
mVelocityTracker.addMovement(event);
break;
case MotionEvent.ACTION_MOVE:
mVelocityTracker.addMovement(event);
// When you want to determine the velocity, call
// computeCurrentVelocity(). Then call getXVelocity()
// and getYVelocity() to retrieve the velocity for each pointer ID.
mVelocityTracker.computeCurrentVelocity(1000);
break;
case MotionEvent.ACTION_UP:
finalME=event;
break;
case MotionEvent.ACTION_CANCEL:
// Return a VelocityTracker object back to be re-used by others.
mVelocityTracker.recycle();
break;
}
return onFling(initialME, finalME, mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity());
//return false;
}
};
mView3.setOnTouchListener(gestureListener);
}
@Override
public boolean onTouchEvent(MotionEvent me) {
System.out.println("Inside onTouchEvent() of GenesMotionDetector.java");
return gestureScanner.onTouchEvent(me);
}
@Override
public boolean onDown(MotionEvent e) {
System.out.println("Inside onDown() of GenesMotionDetector.java");
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
System.out.println("Inside onFling() of GenesMotionDetector.java");
System.out.println("e1= "+ e1);
System.out.println("e2= "+ e2);
System.out.println("velocityX= "+ velocityX);
System.out.println("velocityY= "+ velocityY);
return true;
}
@Override
public void onLongPress(MotionEvent e) {
System.out.println("Inside onLongPress() of GenesMotionDetector.java");
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
System.out.println("Inside onScroll() of GenesMotionDetector.java");
return true;
}
@Override
public void onShowPress(MotionEvent e) {
System.out.println("Inside onShowPress() of GenesMotionDetector.java");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
System.out.println("Inside onSingleTapUp() of GenesMotionDetector.java");
return true;
}
}
puede ser que también acaba manejar todo en onTouch(). Básicamente obtengo los gestos en Toque y pasé a onFling().
Véase también [Cómo agregar un detector gesto a una vista en Android] (https://stackoverflow.com/questions/45054908/cómo hacerlo add-a-gesture-detector-to-a-view-in-android) – Suragch