zoukankan      html  css  js  c++  java
  • 【Android Developers Training】 67. 响应触摸事件

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好。

    原文链接:http://developer.android.com/training/graphics/opengl/touch.html


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

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


    一). 配置触摸监听器

    为了让你的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);
    }

    二). 公开旋转角度

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

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

    三). 应用旋转

    为了应用触摸输入所导致的旋转,注释掉创建一个旋转角度的代码,然后添加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. 由触摸输入所旋转的三角形(圆形代表了当前触摸位置)

  • 相关阅读:
    3.6 符号表的应用
    将博客搬至CSDN
    webpack打包vue项目IE报错,“对象不支持“use”属性或方法”
    移动端解决input被输入法挡住的问题
    javascript中对象的深复制的几种方法
    如何随机洗牌一个数组
    setInterval中this指向的问题
    css中的各种常见布局写法
    vue设置全局变量或函数
    【nodejs爬虫】使用async控制并发写一个小说爬虫
  • 原文地址:https://www.cnblogs.com/jdneo/p/3557737.html
Copyright © 2011-2022 走看看