How to intercept touchscreen events in Android OpenGL ES?

喜夏-厌秋 提交于 2020-01-06 03:54:04

问题


How exactly do you intercept touchscreen events for OpenGL ES games in Android? Also, if the game is 3D, how do you know if the user touched an object in the background? Thanks.


回答1:


Override onTouchEvent(MotionEvent e) into your class extending GlsurfaceView.

  @Override public boolean onTouchEvent(MotionEvent e) {
        float x = e.getX();
        float y = e.getY();
        switch (e.getAction()) {
        case MotionEvent.ACTION_MOVE:
            float dx = x - mPreviousX;
            float dy = y - mPreviousY;
            mRenderer.mAngleX += dx * TOUCH_SCALE_FACTOR;
            mRenderer.mAngleY += dy * TOUCH_SCALE_FACTOR;
            requestRender();
        }
        mPreviousX = x;
        mPreviousY = y;
        return true;
    }

And to know the object touch by user you should compare touch event coordinates with object coordinates.



来源:https://stackoverflow.com/questions/5453855/how-to-intercept-touchscreen-events-in-android-opengl-es

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