注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術(shù)一般,由于喜愛安卓而產(chǎn)生了翻譯的念頭,純屬個(gè)人興趣愛好。
原文鏈接: http://developer.android.com/training/graphics/opengl/touch.html
讓對(duì)象根據(jù)預(yù)設(shè)的程序運(yùn)動(dòng),如讓一個(gè)三角形旋轉(zhuǎn)可以有效地讓人引起注意,但是如果你希望可以讓OpenGL ES與用戶交互呢?讓你的OpenGL ES應(yīng)用可以與觸摸交互的關(guān)鍵點(diǎn)在于,拓展你的 GLSurfaceView 的實(shí)現(xiàn),覆寫 onTouchEvent() 方法來監(jiān)聽觸摸事件。
這節(jié)課將會(huì)向你展示如何監(jiān)聽觸摸事件,讓用戶旋轉(zhuǎn)一個(gè)OpenGL ES對(duì)象。
一). 配置觸摸監(jiān)聽器
為了讓你的OpenGL ES應(yīng)用響應(yīng)觸摸事件,你必須實(shí)現(xiàn)在
GLSurfaceView
類中的
onTouchEvent()
方法。下述實(shí)現(xiàn)的樣例展示了如何監(jiān)聽
MotionEvent.ACTION_MOVE
事件,并將它們轉(zhuǎn)換為形狀旋轉(zhuǎn)的角度:
@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 ; }
注意在計(jì)算旋轉(zhuǎn)角度后,該方法會(huì)調(diào)用 requestRender() 來告訴渲染器現(xiàn)在可以進(jìn)行渲染了。該方法對(duì)于這個(gè)例子來說是最有效的,因?yàn)閳D形并不需要重新繪制,除非有一個(gè)旋轉(zhuǎn)角度的變化。然而,它對(duì)于執(zhí)行效率并沒有任何影響,除非你需要渲染器僅在數(shù)據(jù)變化時(shí)才會(huì)重新繪制(使用 setRenderMode() 方法),所以請(qǐng)確保這一行沒有被注釋掉:
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)的角度,方法是在你的渲染器中添加一個(gè)共有成員。由于渲染器代碼運(yùn)行在一個(gè)獨(dú)立的線程中(非主UI線程),你必須將你的這個(gè)公共變量聲明為 volatile。請(qǐng)看下面的代碼:
public class MyGLRenderer implements GLSurfaceView.Renderer { ... public volatile float mAngle;
三). 應(yīng)用旋轉(zhuǎn)
為了應(yīng)用觸摸輸入所導(dǎo)致的旋轉(zhuǎn),注釋掉創(chuàng)建一個(gè)旋轉(zhuǎn)角度的代碼,然后添加 mAngle,該變量包含了輸入所導(dǎo)致的角度:
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)行這個(gè)程序并用你的手指在屏幕上拖動(dòng),來旋轉(zhuǎn)三角形:
圖1. 由觸摸輸入所旋轉(zhuǎn)的三角形(圓形代表了當(dāng)前觸摸位置)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
