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

當你完成上面介紹的步驟,運行你的程序,然后在屏幕上拖拽你的手指來旋轉這個三角形。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM