我正在尝试在我的应用中的ImageView上使用简单的滑动/拖动手势识别器。但是,我没有看到任何东西。任何人都可以看看代码,让我知道我在这里缺少什么?感谢。
注1:从我的调试中,我可以看到执行确实在GestureListener中。但是,从那里得到的回报是假的。
代码:
public class ViewSuccess extends Activity {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
ImageView movieFrame;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
imageLoader=new ImageLoader(getApplicationContext());
movieFrame = (ImageView) findViewById(R.id.widget32);
movieFrame.setImageResource(R.drawable.sample);
// Gesture detection
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
movieFrame.setOnTouchListener(gestureListener);
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
System.out.println("in gesture recognizer !!!");
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(ViewSuccess.this, "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(ViewSuccess.this, "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}
}
}
答案 0 :(得分:2)
让它工作....我们需要将以下方法添加到SimpleOnGestureListener类
@Override
public boolean onDown(MotionEvent e)
{
return true;
}