【Android Developers Training】 67. 响应触摸事件

注:本文翻译自Google官方的Android Developers Training文档,译者技术通常,因为喜好安卓而产生了翻译的念头,纯属我的兴趣爱好。html

原文连接:http://developer.android.com/training/graphics/opengl/touch.htmlandroid


让对象根据预设的程序运动,如让一个三角形旋转能够有效地让人引发注意,可是若是你但愿可让OpenGL ES与用户交互呢?让你的OpenGL ES应用能够与触摸交互的关键点在于,拓展你的GLSurfaceView的实现,覆写onTouchEvent()方法来监听触摸事件。ide

这节课将会向你展现如何监听触摸事件,让用户旋转一个OpenGL ES对象。this


一). 配置触摸监听器spa

为了让你的OpenGL ES应用响应触摸事件,你必须实如今GLSurfaceView类中的onTouchEvent()方法。下述实现的样例展现了如何监听MotionEvent.ACTION_MOVE事件,并将它们转换为形状旋转的角度:线程

@Override
public boolean onTouchEvent(MotionEvent e) {
    // MotionEvent reports input details from the touch screen
    // and other input controls. In this case, you are only
    // interested in events where the touch position changed.

    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.setAngle(
                    mRenderer.getAngle() +
                    ((dx + dy) * TOUCH_SCALE_FACTOR);  // = 180.0f / 320
            requestRender();
    }

    mPreviousX = x;
    mPreviousY = y;
    return true;
}

注意在计算旋转角度后,该方法会调用requestRender()来告诉渲染器如今能够进行渲染了。该方法对于这个例子来讲是最有效的,由于图形并不须要从新绘制,除非有一个旋转角度的变化。然而,它对于执行效率并无任何影响,除非你须要渲染器仅在数据变化时才会从新绘制(使用setRenderMode()方法),因此请确保这一行没有被注释掉:翻译

public MyGLSurfaceView(Context context) {
    ...
    // Render the view only when there is a change in the drawing data
    setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}

二). 公开旋转角度rest

上述样例代码须要你公开旋转的角度,方法是在你的渲染器中添加一个共有成员。因为渲染器代码运行在一个独立的线程中(非主UI线程),你必须将你的这个公共变量声明为volatile。请看下面的代码:code

public class MyGLRenderer implements GLSurfaceView.Renderer {
    ...
    public volatile float mAngle;

三). 应用旋转htm

为了应用触摸输入所致使的旋转,注释掉建立一个旋转角度的代码,而后添加mAngle,该变量包含了输入所致使的角度:

public void onDrawFrame(GL10 gl) {
    ...
    float[] scratch = new float[16];

    // 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);

    // Combine the rotation matrix with the projection and camera view
    // Note that the mMVPMatrix factor *must be first* in order
    // for the matrix multiplication product to be correct.
    Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0);

    // Draw triangle
    mTriangle.draw(scratch);
}

当你完成了上述的步骤,运行这个程序并用你的手指在屏幕上拖动,来旋转三角形:

图1. 由触摸输入所旋转的三角形(圆形表明了当前触摸位置)

相关文章
相关标签/搜索