zoukankan      html  css  js  c++  java
  • Android OpenGL ES 开发(七): OpenGL ES 响应触摸事件

    像旋转三角形一样,通过预设程序来让对象移动对于吸引注意是很有用的,但是如果你想让你的OpenGL图形有用户交互呢?让你的OpenGL ES应用有触摸交互的关键是,扩展你的GLSurfaceView的实现重载onTouchEvent()方法来监听触摸事件。
    本节内容将向你展示如何监听触摸事件来让用户旋转一个图形。

    一、设置触摸事件

    为了你的OpenGL ES应用能够响应触摸事件,你必须在你的GLSurfaceView中实现onTouchEvent()方法,下面的实现例子展示了怎样监听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);
    }
    

    当你完成上面介绍的步骤,运行你的程序,然后在屏幕上拖拽你的手指来旋转这个三角形。

  • 相关阅读:
    纯js面试题
    js面试题汇总:
    css面试题-圣杯布局
    6.1 React组件 Component的两个问题
    7.1 React异步加载
    vscode插件配置jsx:
    6.2 React renderProps 和 HOC组件优化
    2.5 React高阶组件HOC
    04 Python学习之数据类型-bytes
    03 Python学习之Python基础
  • 原文地址:https://www.cnblogs.com/renhui/p/8005528.html
Copyright © 2011-2022 走看看