您好,登錄后才能下訂單哦!
這篇文章主要介紹“Android自定義view實現(xiàn)3D正方體效果”,在日常操作中,相信很多人在Android自定義view實現(xiàn)3D正方體效果問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Android自定義view實現(xiàn)3D正方體效果”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習吧!
前言
一、小提
二、將傳感器改成事件分發(fā)機制
三、使用
四、源碼
在之前寫了一篇關(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); }
@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è)置投影矩陣。但并不需要在每次繪制時都做,通常情況下,當視圖調(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坐標系中采用的是3維坐標: 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圖元可以采用恒定或者光滑著色模式,默認值為光滑著色模式。當圖元進行光柵化的時候,將引起插入頂點顏色計算,不同顏色將被均勻分布到各個像素片段。 //參數(shù): //mode——指明一個符號常量來代表要使用的著色技術(shù)。允許的值有GL_FLAT 和GL_SMOOTH,初始值為GL_SMOOTH。 gl.glShadeModel(GL10.GL_SMOOTH); //定義一個頂點坐標矩陣。 //參數(shù): // //size——每個頂點的坐標維數(shù),必須是2, 3或者4,初始值是4。 // //type——指明每個頂點坐標的數(shù)據(jù)類型,允許的符號常量有GL_BYTE, GL_SHORT, GL_FIXED和GL_FLOAT,初始值為GL_FLOAT。 // //stride——指明連續(xù)頂點間的位偏移,如果為0,頂點被認為是緊密壓入矩陣,初始值為0。 // //pointer——指明頂點坐標的緩沖區(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ù)渲染圖元 //可以事先指明獨立的頂點、法線、顏色和紋理坐標矩陣并且可以通過調(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(); } }
到此,關(guān)于“Android自定義view實現(xiàn)3D正方體效果”的學(xué)習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習,快去試試吧!若想繼續(xù)學(xué)習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。