Android自定義view之3D正方體效果實例
前言
在之前寫了一篇關(guān)于3D效果的文章,借助傳感器展示,有小伙伴問可不可以改成手勢滑動操作(事件分發(fā)),所以出一篇文章
傳感器相關(guān)文章鏈接:Android 3D效果的實現(xiàn)
一、小提
相對于常見的自定義view而言,繼承的GLSurfaceView只有兩個構(gòu)造函數(shù)??梢岳斫鉃闆]有提供獲取自定義屬性的方法。
public TouchSurfaceView(Context context) { super(context); } public TouchSurfaceView(Context context, AttributeSet attrs) { super(context, attrs); }
二、將傳感器改成事件分發(fā)機制
@Override public boolean onTouchEvent(MotionEvent e) { float x = e.getX(); float y = e.getY(); switch (e.getAction()) { case MotionEvent.ACTION_MOVE: float dx = x - mPreviousX; float dy = y - mPreviousY; mRenderer.mAngleX += dx * TOUCH_SCALE_FACTOR; mRenderer.mAngleY += dy * TOUCH_SCALE_FACTOR; requestRender(); } mPreviousX = x; mPreviousY = y; return true; }
要注意還有一個滾動球事件
@Override public boolean onTrackballEvent(MotionEvent e) { mRenderer.mAngleX += e.getX() * TRACKBALL_SCALE_FACTOR; mRenderer.mAngleY += e.getY() * TRACKBALL_SCALE_FACTOR; requestRender(); return true; }
三、使用
mGLSurfaceView = new TouchSurfaceView(this); setContentView(mGLSurfaceView); mGLSurfaceView.requestFocus(); mGLSurfaceView.setFocusableInTouchMode(true);
注意要在對應(yīng)生命周期中處理
@Override protected void onResume() { super.onResume(); mGLSurfaceView.onResume(); } @Override protected void onPause() { super.onPause(); mGLSurfaceView.onPause(); }
四、源碼
TouchSurfaceView.java
除去前面的修改部分,其他大多與鏈接文章相同,僅將傳感器改成了事件分發(fā)。(代碼中難點有注釋)
public class TouchSurfaceView extends GLSurfaceView { private final float TOUCH_SCALE_FACTOR = 180.0f / 320; private final float TRACKBALL_SCALE_FACTOR = 36.0f; private CubeRenderer mRenderer; private float mPreviousX; private float mPreviousY; public TouchSurfaceView(Context context) { super(context); mRenderer = new CubeRenderer(); setRenderer(mRenderer); setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); } public TouchSurfaceView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onTrackballEvent(MotionEvent e) { mRenderer.mAngleX += e.getX() * TRACKBALL_SCALE_FACTOR; mRenderer.mAngleY += e.getY() * TRACKBALL_SCALE_FACTOR; requestRender(); return true; } @Override public boolean onTouchEvent(MotionEvent e) { float x = e.getX(); float y = e.getY(); switch (e.getAction()) { case MotionEvent.ACTION_MOVE: float dx = x - mPreviousX; float dy = y - mPreviousY; mRenderer.mAngleX += dx * TOUCH_SCALE_FACTOR; mRenderer.mAngleY += dy * TOUCH_SCALE_FACTOR; requestRender(); } mPreviousX = x; mPreviousY = y; return true; } private class CubeRenderer implements GLSurfaceView.Renderer { private Cube mCube; public float mAngleX; public float mAngleY; public CubeRenderer() { mCube =new Cube(); } public void onDrawFrame(GL10 gl) { // | GL10.GL_DEPTH_BUFFER_BIT gl.glClear(GL10.GL_COLOR_BUFFER_BIT); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); gl.glTranslatef(0, 0, -3.0f); gl.glRotatef(mAngleX, 0, 1, 0); gl.glRotatef(mAngleY, 1, 0, 0); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_COLOR_ARRAY); mCube.draw(gl); } @Override public void onSurfaceCreated(GL10 gl, javax.microedition.khronos.egl.EGLConfig config) { gl.glDisable(GL10.GL_DITHER); gl.glClearColor(1,1,1,1); } public void onSurfaceChanged(GL10 gl, int width, int height) { gl.glViewport(0, 0, width, height); //設(shè)置投影矩陣。但并不需要在每次繪制時都做,通常情況下,當(dāng)視圖調(diào)整大小時,需要設(shè)置一個新的投影。 float ratio = (float) width / height; gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10); } } public class Cube { //opengl坐標(biāo)系中采用的是3維坐標(biāo): private FloatBuffer mVertexBuffer; private FloatBuffer mColorBuffer; private ByteBuffer mIndexBuffer; public Cube() { final float vertices[] = { -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, }; final float colors[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, }; final byte indices[] = { 0, 4, 5, 0, 5, 1, 1, 5, 6, 1, 6, 2, 2, 6, 7, 2, 7, 3, 3, 7, 4, 3, 4, 0, 4, 7, 6, 4, 6, 5, 3, 0, 1, 3, 1, 2 }; ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4); vbb.order(ByteOrder.nativeOrder()); mVertexBuffer = vbb.asFloatBuffer(); mVertexBuffer.put(vertices); mVertexBuffer.position(0); ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4); cbb.order(ByteOrder.nativeOrder()); mColorBuffer = cbb.asFloatBuffer(); mColorBuffer.put(colors); mColorBuffer.position(0); mIndexBuffer = ByteBuffer.allocateDirect(indices.length); mIndexBuffer.put(indices); mIndexBuffer.position(0); } public void draw(GL10 gl) { //啟用服務(wù)器端GL功能。 gl.glEnable(GL10.GL_CULL_FACE); //定義多邊形的正面和背面。 //參數(shù): //mode——多邊形正面的方向。GL_CW和GL_CCW被允許,初始值為GL_CCW。 gl.glFrontFace(GL10.GL_CW); //選擇恒定或光滑著色模式。 //GL圖元可以采用恒定或者光滑著色模式,默認(rèn)值為光滑著色模式。當(dāng)圖元進行光柵化的時候,將引起插入頂點顏色計算,不同顏色將被均勻分布到各個像素片段。 //參數(shù): //mode——指明一個符號常量來代表要使用的著色技術(shù)。允許的值有GL_FLAT 和GL_SMOOTH,初始值為GL_SMOOTH。 gl.glShadeModel(GL10.GL_SMOOTH); //定義一個頂點坐標(biāo)矩陣。 //參數(shù): // //size——每個頂點的坐標(biāo)維數(shù),必須是2, 3或者4,初始值是4。 // //type——指明每個頂點坐標(biāo)的數(shù)據(jù)類型,允許的符號常量有GL_BYTE, GL_SHORT, GL_FIXED和GL_FLOAT,初始值為GL_FLOAT。 // //stride——指明連續(xù)頂點間的位偏移,如果為0,頂點被認(rèn)為是緊密壓入矩陣,初始值為0。 // //pointer——指明頂點坐標(biāo)的緩沖區(qū),如果為null,則沒有設(shè)置緩沖區(qū)。 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer); //定義一個顏色矩陣。 //size指明每個顏色的元素數(shù)量,必須為4。type指明每個顏色元素的數(shù)據(jù)類型,stride指明從一個顏色到下一個允許的頂點的字節(jié)增幅,并且屬性值被擠入簡單矩陣或存儲在單獨的矩陣中(簡單矩陣存儲可能在一些版本中更有效率)。 gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer); //由矩陣數(shù)據(jù)渲染圖元 //可以事先指明獨立的頂點、法線、顏色和紋理坐標(biāo)矩陣并且可以通過調(diào)用glDrawElements方法來使用它們創(chuàng)建序列圖元。 gl.glDrawElements(GL10.GL_TRIANGLES, 36, GL10.GL_UNSIGNED_BYTE, mIndexBuffer); } } }
MainActivity.java
public class MainActivity extends AppCompatActivity { private GLSurfaceView mGLSurfaceView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mGLSurfaceView = new TouchSurfaceView(this); setContentView(mGLSurfaceView); mGLSurfaceView.requestFocus(); mGLSurfaceView.setFocusableInTouchMode(true); } @Override protected void onResume() { super.onResume(); mGLSurfaceView.onResume(); } @Override protected void onPause() { super.onPause(); mGLSurfaceView.onPause(); } }
總結(jié)
到此這篇關(guān)于Android自定義view之3D正方體效果的文章就介紹到這了,更多相關(guān)Android自定義view之3D正方體內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android高級界面組件之拖動條和評星條的功能實現(xiàn)
這篇文章主要介紹了Android高級界面組件之拖動條和評星條的實現(xiàn)實例,需要的的朋友參考下2017-03-03android項目從Eclipse遷移到Android studio中常見問題解決方法
android項目從Eclipse遷移到Android studio中經(jīng)常會遇到一些問題,本文提供了Android studio使用中常見問題解決方法2018-03-03android仿知乎ScrollView滾動改變標(biāo)題欄透明度
這篇文章主要為大家詳細(xì)介紹了android仿知乎ScrollView滾動改變標(biāo)題欄透明度,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06根據(jù)USER-AGENT判斷手機類型并跳轉(zhuǎn)到相應(yīng)的app下載頁面
檢測瀏覽器的USER-AGENT,然后根據(jù)正則表達式來確定客戶端類型,并跳轉(zhuǎn)到相應(yīng)的app下載頁面,這個方法還是比較實用的,大家可以看看2014-09-09Android獲取網(wǎng)絡(luò)圖片并顯示的方法
這篇文章主要為大家詳細(xì)介紹了Android獲取網(wǎng)絡(luò)圖片并顯示的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11Android傳感器SensorEventListener之加速度傳感器
今天小編就為大家分享一篇關(guān)于Android傳感器SensorEventListener之加速度傳感器,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02Android仿網(wǎng)易新聞圖片詳情下滑隱藏效果示例代碼
這篇文章主要給大家介紹了關(guān)于利用Android如何仿網(wǎng)易新聞圖片詳情下滑隱藏效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07