Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9127
  • 博文数量: 12
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 150
  • 用 户 组: 普通用户
  • 注册时间: 2015-02-28 14:06
文章分类

全部博文(12)

文章存档

2015年(12)

我的朋友
最近访客

分类: Android平台

2015-03-04 13:34:41

  响应触摸事件

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

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

  设置一个触摸监听器

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

  @Override

  public 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要渲染帧了。这样做是很高效的,因为在没有发生旋转时不需要重画帧。然而,在你没有要求只在数据发生改变才重画之前,还不能达到最高效,即别忘了解除这一句的注释:

  publicMyGLSurfaceView(Context context) {

  ...

  // Render the view only when there is a change in the drawing data

  setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

  }


  曝露出旋转角度

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

  public class MyGLRenderer implements GLSurfaceView.Renderer {

  ...

  public volatile float mAngle;

  }

  应用旋转

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

  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. 跟据触摸输入的转动的三角形(圈圈显示了触摸的位置)。
阅读(301) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~