简体   繁体   中英

how to handle touch events in android with opengl

How to handle touchscreen events for OpenGl programs in Android? I have checked out several posts related to this question, like- How to intercept touchscreen events in Android OpenGL ES? But my problem is that I am only able to handle MotionEvent.ACTION_DOWN events, even if I write the handler for other type of events, those handlers are not called.Android docs sample also uses this event only.Are the other events supported?? My onTouch handler looks something like this-

public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN :
      Log.d(TAG, "mode=DOWN");
      break;
      case MotionEvent.ACTION_UP :
      case MotionEvent.ACTION_POINTER_UP :
      Log.d(TAG, "mode=UP");
          break;
      }
   return true;
}

Try doing a series of if/else statements and see if they work, then try converting it to a switch statement. Maybe take out break and instead return true.

This is how I do it:

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            final float mouseSensitivity = 0.5f;

            if(event.getAction()==MotionEvent.ACTION_DOWN){
                startX = event.getX();
                startY = event.getY();
            } else if(event.getAction()==MotionEvent.ACTION_UP){
                startX = 0.0f;
                startY = 0.0f;
            } else if(event.getAction()==MotionEvent.ACTION_MOVE){
                graphicsRenderer.rotate((startY - event.getY()) * mouseSensitivity,(startX - event.getX()) * mouseSensitivity);
                startX = event.getX();
                startY = event.getY();
            }

            return true;
        }

// Where startX and startY are global int variables;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM