2013-03-02 84 views
5

我用libgdx在屏幕中央显示了一个图像。如果我向左滑动,图像应该左移,如果我刷右图像应该右移Android libgdx使用手势监听器轻扫左右检测

后续向左滑动应向左移动图像。同样的情况也会发生。我用GestureListener

它在某种程度上在某种程度上起作用,如果我向左滑动左移第一个图像。但之后,如果我尝试向右滑动,图像仍然向左移动。

那么我如何克服这个在libgdx?

class MyGestureListener implements GestureListener { 

     @Override 
     public boolean fling(float arg0, float arg1, int arg2) { 
      // TODO Auto-generated method stub 


       if(arg0>0) 
       iX += 20; 
       else 
      // else if(arg0*100>iX) 
        iX-=20; 
       System.out.println("Hello..............."+iX); 
      return true; 
     } 

    Gdx.input.setInputProcessor(new GestureDetector(0.0f, 0.0f,0.0f, 5f,new MyGestureListener())); 

    batch.draw(splashTexture, iX, iY); 
+0

您可以发布您的代码段... – 2013-03-02 17:26:44

+0

你可以让你从[这个老帖] [1] [1]回答:http://stackoverflow.com/questions/937313/android-basic-gesture-detection – 2013-03-02 17:31:26

+0

这是否适用于libgdx? – Raghunandan 2013-03-02 17:33:00

回答

4

我在这个链接中使用了示例。 https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/GestureDetectorTest.java

@Override 
    public boolean fling(float velocityX, float velocityY, int button) { 
     if(Math.abs(velocityX)>Math.abs(velocityY)){ 
       if(velocityX>0){ 
         iX+=20;//x cordinate 
       }else if (velocityX<0){ 
         iX-=20; 
       } else { 
       // Do nothing. 
       } 
     }else{ 

      // Ignore the input, because we don't care about up/down swipes. 
     } 
return true; 
} 
+5

为什么这是低调? – Raghunandan 2014-05-31 07:04:57

0
public class Test extends Activity{ 

private GestureDetector gesturedetector = null; 

View layout; 

@Override 

public void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 

setContentView(R.layout.test); 

layout = (LinearLayout)findViewById(R.id.container); 

gesturedetector = new GestureDetector(new MyGestureListener()); 

layout.setOnTouchListener(new OnTouchListener() { 

@Override 

public boolean onTouch(View v, MotionEvent event) { 

gesturedetector.onTouchEvent(event); 

return true; 

} 

}); 

} 

public boolean dispatchTouchEvent(MotionEvent ev){ 

super.dispatchTouchEvent(ev); 

return gesturedetector.onTouchEvent(ev); 

} 

class MyGestureListener extends GestureDetector.SimpleOnGestureListener{ 

private static final int SWIPE_MIN_DISTANCE = 150; 

private static final int SWIPE_MAX_OFF_PATH = 100; 

private static final int SWIPE_THRESHOLD_VELOCITY = 100; 

@Override 

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 

float velocityY) { 

float dX = e2.getX()-e1.getX(); 

float dY = e1.getY()-e2.getY(); 

if (Math.abs(dY)<SWIPE_MAX_OFF_PATH && 

Math.abs(velocityX)>=SWIPE_THRESHOLD_VELOCITY && 

Math.abs(dX)>=SWIPE_MIN_DISTANCE) { 

if (dX>0) { 

Toast.makeText(getApplicationContext(), “Right Swipe”, Toast.LENGTH_SHORT).show(); 

} else { 

Toast.makeText(getApplicationContext(), “Left Swipe”, Toast.LENGTH_SHORT).show(); 

} 

return true; 

} else if (Math.abs(dX)<SWIPE_MAX_OFF_PATH && 

Math.abs(velocityY)>=SWIPE_THRESHOLD_VELOCITY && 

Math.abs(dY)>=SWIPE_MIN_DISTANCE) { 

if (dY>0) { 

Toast.makeText(getApplicationContext(), “Up Swipe”, Toast.LENGTH_SHORT).show(); 

} else { 

Toast.makeText(getApplicationContext(), “Down Swipe”, Toast.LENGTH_SHORT).show(); 

} 

return true; 

} 

return false; 

} 

} 

} 
+0

这个答案适用于vanilla Android,不适用于Libgdx。它也没有做任何原始问题没有做的事情。 – 2014-06-19 06:02:17

+0

没有解释只是代码。你能否解释你做了什么代码工作并解决了问题 – 2014-06-19 06:33:33