1page.title= Responding to Touch Events 2parent.title=Displaying Graphics with OpenGL ES 3parent.link=index.html 4 5trainingnavtop=true 6previous.title=Adding Motion 7previous.link=motion.html 8 9@jd:body 10 11<div id="tb-wrapper"> 12<div id="tb"> 13 14<h2>This lesson teaches you to</h2> 15<ol> 16 <li><a href="#listener">Setup a Touch Listener</a></li> 17 <li><a href="#angle">Expose the Rotation Angle</a></li> 18 <li><a href="#rotate">Apply Rotation</a></li> 19</ol> 20 21<h2>You should also read</h2> 22<ul> 23 <li><a href="{@docRoot}guide/topics/graphics/opengl.html">OpenGL</a></li> 24</ul> 25 26<div class="download-box"> 27 <a href="http://developer.android.com/shareables/training/OpenGLES.zip" 28class="button">Download the sample</a> 29 <p class="filename">OpenGLES.zip</p> 30</div> 31 32</div> 33</div> 34 35<p>Making objects move according to a preset program like the rotating triangle is useful for 36getting some attention, but what if you want to have users interact with your OpenGL ES graphics? 37The key to making your OpenGL ES application touch interactive is expanding your implementation of 38{@link android.opengl.GLSurfaceView} to override the {@link 39android.opengl.GLSurfaceView#onTouchEvent onTouchEvent()} to listen for touch events.</p> 40 41<p>This lesson shows you how to listen for touch events to let users rotate an OpenGL ES object.</p> 42 43 44<h2 id="listener">Setup a Touch Listener</h2> 45 46<p>In order to make your OpenGL ES application respond to touch events, you must implement the 47{@link android.opengl.GLSurfaceView#onTouchEvent onTouchEvent()} method in your 48{@link android.opengl.GLSurfaceView} class. The example implementation below shows how to listen for 49{@link android.view.MotionEvent#ACTION_MOVE MotionEvent.ACTION_MOVE} events and translate them to 50an angle of rotation for a shape.</p> 51 52<pre> 53@Override 54public boolean onTouchEvent(MotionEvent e) { 55 // MotionEvent reports input details from the touch screen 56 // and other input controls. In this case, you are only 57 // interested in events where the touch position changed. 58 59 float x = e.getX(); 60 float y = e.getY(); 61 62 switch (e.getAction()) { 63 case MotionEvent.ACTION_MOVE: 64 65 float dx = x - mPreviousX; 66 float dy = y - mPreviousY; 67 68 // reverse direction of rotation above the mid-line 69 if (y > getHeight() / 2) { 70 dx = dx * -1 ; 71 } 72 73 // reverse direction of rotation to left of the mid-line 74 if (x < getWidth() / 2) { 75 dy = dy * -1 ; 76 } 77 78 mRenderer.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR; // = 180.0f / 320 79 requestRender(); 80 } 81 82 mPreviousX = x; 83 mPreviousY = y; 84 return true; 85} 86</pre> 87 88<p>Notice that after calculating the rotation angle, this method calls {@link 89android.opengl.GLSurfaceView#requestRender requestRender()} to tell the 90renderer that it is time to render the frame. This approach is the most efficient in this example 91because the frame does not need to be redrawn unless there is a change in the rotation. However, it 92does not have any impact on efficiency unless you also request that the renderer only redraw when 93the data changes using the {@link android.opengl.GLSurfaceView#setRenderMode setRenderMode()} 94method, so make sure this line is uncommented in the renderer:</p> 95 96<pre> 97public MyGLSurfaceView(Context context) { 98 ... 99 // Render the view only when there is a change in the drawing data 100 <strong>setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);</strong> 101} 102</pre> 103 104<h2 id="angle">Expose the Rotation Angle</h2> 105 106<p>The example code above requires that you expose the rotation angle through your renderer by 107adding a public member. Since the renderer code is running on a separate thread from the main user 108interface thread of your application, you must declare this public variable as {@code volatile}. 109Here is the code to do that:</p> 110 111<pre> 112public class MyGLRenderer implements GLSurfaceView.Renderer { 113 ... 114 public volatile float mAngle; 115</pre> 116 117 118<h2 id="rotate">Apply Rotation</h2> 119 120<p>To apply the rotation generated by touch input, comment out the code that generates an angle and 121add {@code mAngle}, which contains the touch input generated angle:</p> 122 123<pre> 124public void onDrawFrame(GL10 gl) { 125 ... 126 // Create a rotation for the triangle 127 // long time = SystemClock.uptimeMillis() % 4000L; 128 // float angle = 0.090f * ((int) time); 129 <strong>Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);</strong> 130 131 // Combine the rotation matrix with the projection and camera view 132 Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0); 133 134 // Draw triangle 135 mTriangle.draw(mMVPMatrix); 136} 137</pre> 138 139<p>When you have completed the steps described above, run the program and drag your finger over the 140screen to rotate the triangle:</p> 141 142<img src="{@docRoot}images/opengl/ogl-triangle-touch.png"> 143<p class="img-caption"> 144<strong>Figure 1.</strong> Triangle being rotated with touch input (circle shows touch 145location).</p> 146