问题
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