自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android---OpenGL ES之響應(yīng)觸屏事件

移動(dòng)開(kāi)發(fā) Android
本文介紹如何監(jiān)聽(tīng)觸碰事件,讓用戶可以旋轉(zhuǎn)OpenGL ES對(duì)象。為了讓你的OpenGL ES應(yīng)用程序響應(yīng)觸碰事件,你必須在你GLSurfaceView類中實(shí)現(xiàn)onTouchEvent()事件。

像旋轉(zhuǎn)三角形那樣,讓對(duì)象根據(jù)預(yù)設(shè)的程序來(lái)移動(dòng),以便有助于獲取人們的關(guān)注,但是如 果想要讓你的OpenGL ES圖形跟用戶交互,應(yīng)該怎樣做呢?要讓你的OpenGL ES應(yīng)用程序能夠觸碰交互的關(guān)鍵是擴(kuò)展你的GLSurfaceView實(shí)現(xiàn),重寫(xiě)它的onTouchEvent()方法來(lái)監(jiān)聽(tīng)觸碰事件。

本文介紹如何監(jiān)聽(tīng)觸碰事件,讓用戶可以旋轉(zhuǎn)OpenGL ES對(duì)象。

設(shè)置觸碰監(jiān)聽(tīng)器

為了讓你的OpenGL ES應(yīng)用程序響應(yīng)觸碰事件,你必須在你GLSurfaceView類中實(shí)現(xiàn)onTouchEvent()事件。以下實(shí)現(xiàn)的示例顯示如何監(jiān)聽(tīng)MotionEvent.ACTION_MOVE事件,并把它們轉(zhuǎn)換成圖形旋轉(zhuǎn)的角度。

  1. @Override 
  2.   public boolean onTouchEvent(MotionEvent e) { 
  3.   // MotionEvent reportsinput details from the touch screen 
  4.   // and other inputcontrols. In this case, you are only 
  5.   // interested in eventswhere the touch position changed. 
  6.   float x = e.getX(); 
  7.   float y = e.getY(); 
  8.   switch (e.getAction()) { 
  9.   case MotionEvent.ACTION_MOVE: 
  10.   float dx = x - mPreviousX; 
  11.   float dy = y - mPreviousY; 
  12.   // reverse direction of rotation above the mid-line 
  13.   if (y > getHeight() / 2) { 
  14.   dx = dx * -1 ; 
  15.   } 
  16.   // reverse direction of rotation to left of the mid-line 
  17.   if (x < getWidth() / 2) { 
  18.   dy = dy * -1 ; 
  19.   } 
  20.   mRenderer.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR; // = 180.0f /320 
  21.   requestRender(); 
  22.   } 
  23.   mPreviousX = x; 
  24.   mPreviousY = y; 
  25.   return true
  26.   } 

注意,計(jì)算旋轉(zhuǎn)的角度之后,這個(gè)方法調(diào)用了requestRender()方法來(lái)告訴渲 染器,到了渲染幀的時(shí)候了。上例中所使用的方法是最有效的,只有在有旋轉(zhuǎn)變化時(shí),幀才會(huì)被重繪。但是要想只在數(shù)據(jù)變化的時(shí)候,才請(qǐng)求渲染器重繪,就要使用 setRenderMode()方法來(lái)設(shè)置繪制模式。

  1. publicMyGLSurfaceView(Context context){ 
  2.   ... 
  3.   // Render the view onlywhen there is a change in the drawing data 
  4.   setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); 
  5.   } 

暴露旋轉(zhuǎn)的角度

上例代碼要求你通過(guò)添加一個(gè)公共的成員變量,通過(guò)渲染器把旋轉(zhuǎn)的角度暴露出來(lái)。因?yàn)殇秩酒鞔a運(yùn)行在一個(gè)獨(dú)立于主用戶界面線程之外的線程中,所以你必須聲明一個(gè)公共變量,代碼如下:

  1. publicclassMyGLRendererimplementsGLSurfaceView.Renderer{ 
  2.   ... 
  3.   public volatile float mAngle; 

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

以下代碼完成由觸碰輸入所產(chǎn)生的旋轉(zhuǎn):

  1. publicvoidonDrawFrame(GL10 gl){ 
  2.   ... 
  3.   // Create a rotation forthe triangle 
  4.   // long time =SystemClock.uptimeMillis() % 4000L; 
  5.   // float angle = 0.090f *((int) time); 
  6.   Matrix.setRotateM(mRotationMatrix, 0, mAngle, 00, -1.0f); 
  7.   // Combine the rotationmatrix with the projection and camera view 
  8.   Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0); 
  9.   // Draw triangle 
  10.   mTriangle.draw(mMVPMatrix); 
  11.   } 

本文譯自:http://developer.android.com/training/graphics/opengl/touch.html

責(zé)任編輯:閆佳明 來(lái)源: bbs.9ria
相關(guān)推薦

2014-04-29 14:05:02

OpenGL ESAndroid添加動(dòng)作

2014-04-29 14:08:40

OpenGL ESAndroid應(yīng)用投影

2013-04-15 15:22:06

2011-07-28 10:26:18

2014-04-29 14:16:54

2010-09-10 10:09:26

Android

2014-04-29 14:27:59

OpenGL ES 2Android繪制紋理

2014-04-24 13:35:11

OpenGL ES2.iOSAndroid

2017-04-25 09:04:16

2014-04-24 11:16:00

OpenGL ES 2入門

2009-05-28 09:35:52

2014-02-10 09:30:14

Windows 8.1

2011-03-21 15:23:24

觸屏網(wǎng)頁(yè)設(shè)計(jì)

2014-04-29 14:49:37

OpenGL ES 2Android應(yīng)用投影

2013-01-11 13:30:38

觸屏智能手機(jī)新聞閱讀

2013-05-16 15:08:33

2017-07-24 14:32:49

2013-07-05 14:45:05

AndroidOpenGL ES開(kāi)發(fā)

2017-07-19 15:25:16

iOS開(kāi)發(fā)ARKitOpen GL

2023-10-12 22:44:16

iOS事件響應(yīng)鏈
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)