Add Pointer capture demo to APIDemos
Bug: 30897034 Test: manually in ApiDemos > Graphics > OpenGL ES > Touch Rotate Change-Id: Id87b17938cf876b4886de69ffaa8a2b3c4f27143
This commit is contained in:
@@ -16,9 +16,6 @@
|
|||||||
|
|
||||||
package com.example.android.apis.graphics;
|
package com.example.android.apis.graphics;
|
||||||
|
|
||||||
import javax.microedition.khronos.egl.EGLConfig;
|
|
||||||
import javax.microedition.khronos.opengles.GL10;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.opengl.GLSurfaceView;
|
import android.opengl.GLSurfaceView;
|
||||||
@@ -27,6 +24,9 @@ import android.view.InputDevice;
|
|||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
|
|
||||||
|
import javax.microedition.khronos.egl.EGLConfig;
|
||||||
|
import javax.microedition.khronos.opengles.GL10;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper activity demonstrating the use of {@link GLSurfaceView}, a view
|
* Wrapper activity demonstrating the use of {@link GLSurfaceView}, a view
|
||||||
* that uses OpenGL drawing into a dedicated surface.
|
* that uses OpenGL drawing into a dedicated surface.
|
||||||
@@ -35,6 +35,9 @@ import android.view.MotionEvent;
|
|||||||
* + How to redraw in response to user input.
|
* + How to redraw in response to user input.
|
||||||
*/
|
*/
|
||||||
public class TouchRotateActivity extends Activity {
|
public class TouchRotateActivity extends Activity {
|
||||||
|
|
||||||
|
private GLSurfaceView mGLSurfaceView;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@@ -63,44 +66,69 @@ public class TouchRotateActivity extends Activity {
|
|||||||
mGLSurfaceView.onPause();
|
mGLSurfaceView.onPause();
|
||||||
}
|
}
|
||||||
|
|
||||||
private GLSurfaceView mGLSurfaceView;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implement a simple rotation control.
|
* Implement a simple rotation control.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class TouchSurfaceView extends GLSurfaceView {
|
private static class TouchSurfaceView extends GLSurfaceView {
|
||||||
|
|
||||||
public TouchSurfaceView(Context context) {
|
private static final float TOUCH_SCALE_FACTOR = 180.0f / 320;
|
||||||
|
private static final float TRACKBALL_SCALE_FACTOR = 36.0f;
|
||||||
|
private CubeRenderer mRenderer;
|
||||||
|
private float mPreviousX;
|
||||||
|
private float mPreviousY;
|
||||||
|
|
||||||
|
TouchSurfaceView(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
mRenderer = new CubeRenderer();
|
mRenderer = new CubeRenderer();
|
||||||
setRenderer(mRenderer);
|
setRenderer(mRenderer);
|
||||||
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
|
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public boolean onTrackballEvent(MotionEvent e) {
|
@Override
|
||||||
mRenderer.mAngleX += e.getX() * TRACKBALL_SCALE_FACTOR;
|
public boolean onTrackballEvent(MotionEvent e) {
|
||||||
mRenderer.mAngleY += e.getY() * TRACKBALL_SCALE_FACTOR;
|
updateAngles(e.getX(), e.getY(), TRACKBALL_SCALE_FACTOR);
|
||||||
requestRender();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public boolean onTouchEvent(MotionEvent e) {
|
@Override
|
||||||
if (e.getActionMasked() == MotionEvent.ACTION_MOVE) {
|
public boolean onTouchEvent(MotionEvent e) {
|
||||||
updateAngles(e);
|
final int action = e.getActionMasked();
|
||||||
|
if (action == MotionEvent.ACTION_MOVE) {
|
||||||
|
updateAngles(e.getX() - mPreviousX, e.getY() - mPreviousY, TOUCH_SCALE_FACTOR);
|
||||||
|
} else if (action == MotionEvent.ACTION_DOWN) {
|
||||||
|
if (e.isFromSource(InputDevice.SOURCE_MOUSE)) {
|
||||||
|
requestPointerCapture();
|
||||||
|
} else {
|
||||||
|
releasePointerCapture();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
mPreviousX = e.getX();
|
mPreviousX = e.getX();
|
||||||
mPreviousY = e.getY();
|
mPreviousY = e.getY();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateAngles(MotionEvent e) {
|
@Override
|
||||||
float dx = e.getX() - mPreviousX;
|
public boolean onCapturedPointerEvent(MotionEvent e) {
|
||||||
float dy = e.getY() - mPreviousY;
|
if (e.getActionMasked() == MotionEvent.ACTION_DOWN) {
|
||||||
|
releasePointerCapture();
|
||||||
|
} else {
|
||||||
|
updateAngles(e.getX(), e.getY(), TOUCH_SCALE_FACTOR);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||||
|
// Release pointer capture on any key press.
|
||||||
|
releasePointerCapture();
|
||||||
|
return super.onKeyDown(keyCode, event);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateAngles(float dx, float dy, float scaleFactor) {
|
||||||
if (dx != 0 && dy != 0) {
|
if (dx != 0 && dy != 0) {
|
||||||
mRenderer.mAngleX += dx * TOUCH_SCALE_FACTOR;
|
mRenderer.mAngleX += dx * scaleFactor;
|
||||||
mRenderer.mAngleY += dy * TOUCH_SCALE_FACTOR;
|
mRenderer.mAngleY += dy * scaleFactor;
|
||||||
requestRender();
|
requestRender();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,11 +136,12 @@ class TouchSurfaceView extends GLSurfaceView {
|
|||||||
/**
|
/**
|
||||||
* Render a cube.
|
* Render a cube.
|
||||||
*/
|
*/
|
||||||
private class CubeRenderer implements GLSurfaceView.Renderer {
|
private static class CubeRenderer implements GLSurfaceView.Renderer {
|
||||||
public CubeRenderer() {
|
CubeRenderer() {
|
||||||
mCube = new Cube();
|
mCube = new Cube();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void onDrawFrame(GL10 gl) {
|
public void onDrawFrame(GL10 gl) {
|
||||||
/*
|
/*
|
||||||
* Usually, the first thing one might want to do is to clear
|
* Usually, the first thing one might want to do is to clear
|
||||||
@@ -138,6 +167,7 @@ class TouchSurfaceView extends GLSurfaceView {
|
|||||||
mCube.draw(gl);
|
mCube.draw(gl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void onSurfaceChanged(GL10 gl, int width, int height) {
|
public void onSurfaceChanged(GL10 gl, int width, int height) {
|
||||||
gl.glViewport(0, 0, width, height);
|
gl.glViewport(0, 0, width, height);
|
||||||
|
|
||||||
@@ -153,6 +183,7 @@ class TouchSurfaceView extends GLSurfaceView {
|
|||||||
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
|
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
|
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
|
||||||
/*
|
/*
|
||||||
* By default, OpenGL enables features that improve quality
|
* By default, OpenGL enables features that improve quality
|
||||||
@@ -168,20 +199,14 @@ class TouchSurfaceView extends GLSurfaceView {
|
|||||||
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
|
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
|
||||||
GL10.GL_FASTEST);
|
GL10.GL_FASTEST);
|
||||||
|
|
||||||
|
|
||||||
gl.glClearColor(1, 1, 1, 1);
|
gl.glClearColor(1, 1, 1, 1);
|
||||||
gl.glEnable(GL10.GL_CULL_FACE);
|
gl.glEnable(GL10.GL_CULL_FACE);
|
||||||
gl.glShadeModel(GL10.GL_SMOOTH);
|
gl.glShadeModel(GL10.GL_SMOOTH);
|
||||||
gl.glEnable(GL10.GL_DEPTH_TEST);
|
gl.glEnable(GL10.GL_DEPTH_TEST);
|
||||||
}
|
}
|
||||||
private Cube mCube;
|
private Cube mCube;
|
||||||
public float mAngleX;
|
float mAngleX;
|
||||||
public float mAngleY;
|
float mAngleY;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
|
|
||||||
private final float TRACKBALL_SCALE_FACTOR = 36.0f;
|
|
||||||
private CubeRenderer mRenderer;
|
|
||||||
private float mPreviousX;
|
|
||||||
private float mPreviousY;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user