不工作

时间:2012-10-14 02:46:33

标签: android

我试图在投掷的动作上显示不同的图像。为此,我使用了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方法永远不会被调用。我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

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