How to check if the screen touched?

I currently use onTouchEvent(MotionEvent me) to register input events, however this is a game application, when the frame rate slows down, sometimes the program cannot register the input.UP event after the GUI button has been released, which causes my character to continue move on your own ...

There is a logical method in the API that checks if there is any finger on the screen on the screen at any given time.

thanks

+6
source share
4 answers

It might be worth checking out the onUserInteraction () documentation.

Something like this will let you know how the user recently interacted with the screen:

 @Override public void onUserInteraction(){ MyTimerClass.getInstance().resetTimer(); } 
+5
source

You can get the on touch event and see if there will be Action down, Move or Action Up and other actions, but for now let's stop here. I have a simple example that I think you or someone else will like.

 import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.widget.Toast; public class MainActivity extends Activity { private boolean isTouch = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onTouchEvent(MotionEvent event) { int X = (int) event.getX(); int Y = (int) event.getY(); int eventaction = event.getAction(); switch (eventaction) { case MotionEvent.ACTION_DOWN: Toast.makeText(this, "ACTION_DOWN AT COORDS "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show(); isTouch = true; break; case MotionEvent.ACTION_MOVE: Toast.makeText(this, "MOVE "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show(); break; case MotionEvent.ACTION_UP: Toast.makeText(this, "ACTION_UP "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show(); break; } return true; } } 

Greetings

+2
source

There is an OnTouchListener , which has a single onTouch() method for any View .

http://developer.android.com/reference/android/view/View.OnTouchListener.html

0
source

Ensuring that I always return true from OnTouchEvent , unlike super.OnTouchEvent , seems to solve the problem ...

0
source

Source: https://habr.com/ru/post/906438/


All Articles