Android響應(yīng)觸摸事件

2018-08-02 18:37 更新

編寫:jdneo - 原文:http://developer.android.com/training/graphics/opengl/touch.html

讓對象根據(jù)預(yù)設(shè)的程序運(yùn)動(如讓一個三角形旋轉(zhuǎn)),可以有效地引起用戶的注意,但是如果希望讓OpenGL ES的圖形對象與用戶交互呢?讓我們的OpenGL ES應(yīng)用可以支持觸控交互的關(guān)鍵點(diǎn)在于,拓展GLSurfaceView的實(shí)現(xiàn),重寫onTouchEvent()方法來監(jiān)聽觸摸事件。

這節(jié)課將會向你展示如何監(jiān)聽觸控事件,讓用戶旋轉(zhuǎn)一個OpenGL ES對象。

配置觸摸監(jiān)聽器

為了讓我們的OpenGL ES應(yīng)用響應(yīng)觸控事件,我們必須實(shí)現(xiàn)GLSurfaceView類中的onTouchEvent()方法。下面的例子展示了如何監(jiān)聽MotionEvent.ACTION_MOVE事件,并將事件轉(zhuǎn)換為形狀旋轉(zhuǎn)的角度:

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

注意在計(jì)算旋轉(zhuǎn)角度后,該方法會調(diào)用requestRender()來告訴渲染器現(xiàn)在可以進(jìn)行渲染了。這種辦法對于這個例子來說是最有效的,因?yàn)閳D形并不需要重新繪制,除非有一個旋轉(zhuǎn)角度的變化。當(dāng)然,為了能夠真正實(shí)現(xiàn)執(zhí)行效率的提高,記得使用setRenderMode()方法以保證渲染器僅在數(shù)據(jù)發(fā)生變化時才會重新繪制圖形,所以請確保這一行代碼沒有被注釋掉:

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

公開旋轉(zhuǎn)角度

上述樣例代碼需要我們公開旋轉(zhuǎn)的角度,具體來說,是在渲染器中添加一個public成員變量。由于渲染器代碼運(yùn)行在一個獨(dú)立的線程中(非主UI線程),我們必須同時將該變量聲明為volatile。注意下面聲明該變量的代碼,另外對應(yīng)的get和set方法也被聲明為了public成員函數(shù):

public class MyGLRenderer implements GLSurfaceView.Renderer {
    ...

    public volatile float mAngle;

    public float getAngle() {
        return mAngle;
    }

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

應(yīng)用旋轉(zhuǎn)

為了應(yīng)用觸控輸入所生成的旋轉(zhuǎn),注釋掉創(chuàng)建旋轉(zhuǎn)角度的代碼,然后添加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);
}

當(dāng)完成了上述步驟,我們就可以運(yùn)行這個程序,并通過手指在屏幕上的滑動旋轉(zhuǎn)三角形了:

ogl-triangle-touch


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號