图片和图形之回应触摸事件(15)

原文

概要


根据像旋转三角形这样的预设程序使对象移动对于获得某些注意力非常有用,但如果您希望让用户与OpenGL ES图形进行交互,该怎么办?让您的OpenGL ES应用程序触摸交互的关键是扩展您的实现 GLSurfaceView以覆盖 onTouchEvent()侦听触摸事件。

本课向您介绍如何侦听触摸事件以让用户旋转OpenGL ES对象。

设置一个触摸监听器


为了使您的OpenGL ES应用程序响应触摸事件,您必须onTouchEvent()在您的GLSurfaceView课堂上实施该 方法 。下面的示例实现显示了如何侦听 MotionEvent.ACTION_MOVE事件并将它们转换为形状的旋转角度。

private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
private float mPreviousX;
private float mPreviousY;

@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));
            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);
}

公开旋转角度


上面的示例代码要求您通过添加公共成员来显示渲染器的旋转角度。由于渲染器代码正在与应用程序的主用户界面线程分开的线程上运行,因此您必须将此公共变量声明为volatile。这是声明变量并公开getter和setter对的代码:

public class MyGLRenderer implements GLSurfaceView.Renderer {
    ...

    public volatile float mAngle;

    public float getAngle() {
        return mAngle;
    }

    public void setAngle(float angle) {
        mAngle = angle;
    }
}

应用旋转


要应用触摸输入生成的旋转,请注释生成角度和添加的代码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);
}

完成上述步骤后,运行程序并在屏幕上拖动手指以旋转三角形:
图片和图形之回应触摸事件(15)
图1.通过触摸输入旋转的三角形(圆圈显示触摸位置)。

Lastest Update:2018.04.25

联系我

QQ:94297366
微信打赏:https://pan.baidu.com/s/1dSBXk3eFZu3mAMkw3xu9KQ

公众号推荐:

图片和图形之回应触摸事件(15)

猜你喜欢

转载自blog.51cto.com/4789781/2120590
今日推荐