一步一步学android OpenGL ES2.0编程(6 大结局)

来源:互联网 发布:泰拉瑞亚数据修改 编辑:程序博客网 时间:2024/04/30 10:55

响应触摸事件

使你的OpenGL ES应用能响应触摸的关键是扩展你实现的GLSurfaceView 代码,覆写onTouchEvent() 方法来监听触摸事件。

本文向你展示如何监听用户的触摸事件以使用户可以旋转某个OpenGL ES对象。

设置一个触摸监听器

为了使你的OpenGL Es应用响应触摸事件,你必须在你的GLSurfaceView 类中实现onTouchEvent()事件。下面的例子演示了如何监听MotionEvent.ACTION_MOVE 事件然后把它们转换成一个形状的旋转角度。

[java] view plaincopyprint?
  1. @Override  
  2. public boolean onTouchEvent(MotionEvent e) {  
  3.     // MotionEvent带有从触摸屏幕来的输入的详细信息以及其它输入控制  
  4.     // 此处,你只需对触摸位置的改变感兴趣即可。  
  5.   
  6.     float x = e.getX();  
  7.     float y = e.getY();  
  8.   
  9.     switch (e.getAction()) {  
  10.         case MotionEvent.ACTION_MOVE:  
  11.   
  12.             float dx = x - mPreviousX;  
  13.             float dy = y - mPreviousY;  
  14.   
  15.             // reverse direction of rotation above the mid-line  
  16.             if (y > getHeight() / 2) {  
  17.               dx = dx * -1 ;  
  18.             }  
  19.   
  20.             // reverse direction of rotation to left of the mid-line  
  21.             if (x < getWidth() / 2) {  
  22.               dy = dy * -1 ;  
  23.             }  
  24.   
  25.             mRenderer.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR;  // = 180.0f / 320  
  26.             requestRender();  
  27.     }  
  28.   
  29.     mPreviousX = x;  
  30.     mPreviousY = y;  
  31.     return true;  
  32. }  
@Overridepublic boolean onTouchEvent(MotionEvent e) {    // MotionEvent带有从触摸屏幕来的输入的详细信息以及其它输入控制    // 此处,你只需对触摸位置的改变感兴趣即可。    float x = e.getX();    float y = e.getY();    switch (e.getAction()) {        case MotionEvent.ACTION_MOVE:            float dx = x - mPreviousX;            float dy = y - mPreviousY;            // reverse direction of rotation above the mid-line            if (y > getHeight() / 2) {              dx = dx * -1 ;            }            // reverse direction of rotation to left of the mid-line            if (x < getWidth() / 2) {              dy = dy * -1 ;            }            mRenderer.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR;  // = 180.0f / 320            requestRender();    }    mPreviousX = x;    mPreviousY = y;    return true;}

注意在计算完旋转角度之后,本方法调用requestRender() 来告诉renderer要渲染帧了。这样做是很高效的,因为在没有发生旋转时不需要重画帧。然而,在你没有要求只在数据发生改变才重画之前,还不能达到最高效,即别忘了解除这一句的注释:

[java] view plaincopyprint?
  1. public MyGLSurfaceView(Context context) {  
  2.     ...  
  3.     // Render the view only when there is a change in the drawing data  
  4.     setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);  
  5. }  
public MyGLSurfaceView(Context context) {    ...    // Render the view only when there is a change in the drawing data    setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);}

曝露出旋转角度

上面的例子要求你向其它类曝露出你的旋转角度,所以你要为你的renderer添加一个public成员。既然renderer的代码运行于主界面之外的单独线程中,你必须声明这个公开变量为volatile. 。下面的代码就是这样做的:

[java] view plaincopyprint?
  1. public class MyGLRenderer implements GLSurfaceView.Renderer {  
  2.     ...  
  3.     public volatile float mAngle;  
  4. }  
public class MyGLRenderer implements GLSurfaceView.Renderer {    ...    public volatile float mAngle;}

应用旋转

要应用触摸所产生的旋转, 注释掉产生角度的代码并且添加mAngle,它包活了触摸所产生的角度:

[java] view plaincopyprint?
  1. public void onDrawFrame(GL10 gl) {  
  2.     ...  
  3.     // Create a rotation for the triangle  
  4.     // long time = SystemClock.uptimeMillis() % 4000L;  
  5.     // float angle = 0.090f * ((int) time);  
  6.     Matrix.setRotateM(mRotationMatrix, 0, mAngle, 00, -1.0f);  
  7.   
  8.     // 合并旋转矩阵到投影和相机视口矩阵  
  9.     Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);  
  10.   
  11.     // 画一个角度  
  12.     mTriangle.draw(mMVPMatrix);  
  13. }  
public void onDrawFrame(GL10 gl) {    ...    // Create a rotation for the triangle    // long time = SystemClock.uptimeMillis() % 4000L;    // float angle = 0.090f * ((int) time);    Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);    // 合并旋转矩阵到投影和相机视口矩阵    Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);    // 画一个角度    mTriangle.draw(mMVPMatrix);}

当你完成了上述的几步,运行程序然后在陪同幕上拖动你的手指头,你会看到下面这样:


 Figure 1. 跟据触摸输入的转动的三角形(圈圈显示了触摸的位置)。

原创粉丝点击