Android---OpenGL ES之响应触屏事件

移动开发 Android
本文介绍如何监听触碰事件,让用户可以旋转OpenGL ES对象。为了让你的OpenGL ES应用程序响应触碰事件,你必须在你GLSurfaceView类中实现onTouchEvent()事件。

像旋转三角形那样,让对象根据预设的程序来移动,以便有助于获取人们的关注,但是如 果想要让你的OpenGL ES图形跟用户交互,应该怎样做呢?要让你的OpenGL ES应用程序能够触碰交互的关键是扩展你的GLSurfaceView实现,重写它的onTouchEvent()方法来监听触碰事件。

本文介绍如何监听触碰事件,让用户可以旋转OpenGL ES对象。

设置触碰监听器

为了让你的OpenGL ES应用程序响应触碰事件,你必须在你GLSurfaceView类中实现onTouchEvent()事件。以下实现的示例显示如何监听MotionEvent.ACTION_MOVE事件,并把它们转换成图形旋转的角度。

  1. @Override 
  2.   public boolean onTouchEvent(MotionEvent e) { 
  3.   // MotionEvent reportsinput details from the touch screen 
  4.   // and other inputcontrols. In this case, you are only 
  5.   // interested in eventswhere the touch position changed. 
  6.   float x = e.getX(); 
  7.   float y = e.getY(); 
  8.   switch (e.getAction()) { 
  9.   case MotionEvent.ACTION_MOVE: 
  10.   float dx = x - mPreviousX; 
  11.   float dy = y - mPreviousY; 
  12.   // reverse direction of rotation above the mid-line 
  13.   if (y > getHeight() / 2) { 
  14.   dx = dx * -1 ; 
  15.   } 
  16.   // reverse direction of rotation to left of the mid-line 
  17.   if (x < getWidth() / 2) { 
  18.   dy = dy * -1 ; 
  19.   } 
  20.   mRenderer.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR; // = 180.0f /320 
  21.   requestRender(); 
  22.   } 
  23.   mPreviousX = x; 
  24.   mPreviousY = y; 
  25.   return true
  26.   } 

注意,计算旋转的角度之后,这个方法调用了requestRender()方法来告诉渲 染器,到了渲染帧的时候了。上例中所使用的方法是最有效的,只有在有旋转变化时,帧才会被重绘。但是要想只在数据变化的时候,才请求渲染器重绘,就要使用 setRenderMode()方法来设置绘制模式。

  1. publicMyGLSurfaceView(Context context){ 
  2.   ... 
  3.   // Render the view onlywhen there is a change in the drawing data 
  4.   setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); 
  5.   } 

暴露旋转的角度

上例代码要求你通过添加一个公共的成员变量,通过渲染器把旋转的角度暴露出来。因为渲染器代码运行在一个独立于主用户界面线程之外的线程中,所以你必须声明一个公共变量,代码如下:

  1. publicclassMyGLRendererimplementsGLSurfaceView.Renderer{ 
  2.   ... 
  3.   public volatile float mAngle; 

应用旋转

以下代码完成由触碰输入所产生的旋转:

  1. publicvoidonDrawFrame(GL10 gl){ 
  2.   ... 
  3.   // Create a rotation forthe triangle 
  4.   // long time =SystemClock.uptimeMillis() % 4000L; 
  5.   // float angle = 0.090f *((int) time); 
  6.   Matrix.setRotateM(mRotationMatrix, 0, mAngle, 00, -1.0f); 
  7.   // Combine the rotationmatrix with the projection and camera view 
  8.   Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0); 
  9.   // Draw triangle 
  10.   mTriangle.draw(mMVPMatrix); 
  11.   } 

本文译自:http://developer.android.com/training/graphics/opengl/touch.html

责任编辑:闫佳明 来源: bbs.9ria
相关推荐

2014-04-29 14:05:02

OpenGL ESAndroid添加动作

2014-04-29 14:08:40

OpenGL ESAndroid应用投影

2014-04-29 14:16:54

2013-04-15 15:22:06

2011-07-28 10:26:18

2010-09-10 10:09:26

Android

2014-04-29 14:27:59

OpenGL ES 2Android绘制纹理

2014-04-24 13:35:11

OpenGL ES2.iOSAndroid

2014-04-24 11:16:00

OpenGL ES 2入门

2014-04-29 14:49:37

OpenGL ES 2Android应用投影

2017-04-25 09:04:16

2009-05-28 09:35:52

2014-02-10 09:30:14

Windows 8.1

2013-05-16 15:08:33

2011-03-21 15:23:24

触屏网页设计

2013-01-11 13:30:38

触屏智能手机新闻阅读

2013-07-05 14:45:05

AndroidOpenGL ES开发

2014-06-18 10:34:41

Android字体渲染器OpenGL ES

2017-07-19 15:25:16

iOS开发ARKitOpen GL

2017-07-24 14:32:49

点赞
收藏

51CTO技术栈公众号