How to intercept touchscreen events in Android Ope

2019-07-24 05:29发布

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条回答
兄弟一词,经得起流年.
2楼-- · 2019-07-24 06:09

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.

查看更多
登录 后发表回答