0

我试图在投掷动作上显示不同的图像。为此,我使用了 SimpleOnGestureListener。但是一扔是行不通的。大家能帮我找出原因吗。这是代码

public class MainActivity extends Activity{
ImageView m_imageView;
GestureDetector m_gdt;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.v("INFO", "Inside oncreate");

    m_gdt = new GestureDetector(this, new GestureListener());

    m_imageView = (ImageView)findViewById(R.id.imageView1);

    m_imageView.setOnTouchListener(new OnTouchListener() {
        @Override 
        public boolean onTouch(final View view, final MotionEvent event) {
            Log.v("INFO", "Inside onTouch");

            return m_gdt.onTouchEvent(event);
        }
    });

}
} 

public class GestureListener extends SimpleOnGestureListener{

private final int SWIPE_MIN_DISTANCE = 120;
private final int SWIPE_THRESHOLD_VELOCITY = 200;

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    Log.v("INFO", "Fling detected");
    if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        // Right to left, your code here
        Log.v("INFO", "Image moving forward");
        return true;
    } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) >  SWIPE_THRESHOLD_VELOCITY) {
        // Left to right, your code here
        Log.v("INFO", "Image moving backward");
        return true;
    }
    return false;
}
}

显示日志消息“Inside onTouch”。但是 onFling 方法永远不会被调用。我在这里想念什么?

4

1 回答 1

0

使用这个问题的第三个答案(来自 Thomas Fankhauser),它会起作用: Fling gesture detection on grid layout

于 2012-10-14T02:54:58.450 回答