Android SurfaceView: Better way to tell if screen is touched (java)

不想你离开。 提交于 2019-12-11 21:12:42

问题


I need a way to tell if there is at least one finger touching the screen. I had no problem doing this in LibGDX but now I can't do it without it. This is how I have it setup:

public static boolean screenTouched = false;

//This methods is run at 60FPS by the main thread (it's the main game loop)
public static void update(){
    //Run other updates...
    screenTouched = false;//Called after other updates
}

public boolean onTouchEvent(MotionEvent e){
    screenTouched = true;
}

By the way, this is all in the main activity class.

This seems like it would work fine, right? It doesn't, onTouchEvent(MotionEvent e) is only called by android when the user's finger moves on the screen and doesn't care about whether they are just touching it or not. The problem with this is that if the user touches the screen and keeps their finger in place while still touching the screen, onTouchEvent will not be called and screenTouched will stay false. Anyone know a way around this to be able to tell if the screen is being touched even if the users finger isn't moving?

来源:https://stackoverflow.com/questions/25755340/android-surfaceview-better-way-to-tell-if-screen-is-touched-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!