Android实现飘动的旗帜效果实例
生活随笔
收集整理的這篇文章主要介紹了
Android实现飘动的旗帜效果实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
做一個飄動的旗幟效果,思路大概為:將旗形的波浪分割成很小的四邊形,然后多少時間進行刷新,Z方向上運用sin函數改變,看起來有飄動的感覺
實例代碼如下:
1、準備圖片資源 : initBitmap類
2、Activity類
3、渲染類
import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer;import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10;import android.opengl.GLUtils; import android.opengl.GLSurfaceView.Renderer;public class FlagRender implements Renderer {/*** 制作飄動的旗幟效果*/private float[] texVertices = new float[12];FloatBuffer texBuffer;private float[] coord = new float[8];FloatBuffer coordBuffer;private int[] textures = new int[1];float vertex[][][] = new float[45][45][3];// 網格頂點數組int wiggle_count = 0; // 指定旗型波浪的運行速度float hold; // 臨時變量float xrot, yrot, zrot; // 各軸旋轉/*** 初始化緩沖數據*/public void init() {ByteBuffer bb = ByteBuffer.allocateDirect(texVertices.length * 4);bb.order(ByteOrder.nativeOrder());texBuffer = bb.asFloatBuffer();texBuffer.put(texVertices);texBuffer.position(0);//ByteBuffer coordbb = ByteBuffer.allocateDirect(coord.length * 4);coordbb.order(ByteOrder.nativeOrder());coordBuffer = coordbb.asFloatBuffer();coordBuffer.put(coord);coordBuffer.position(0);}@Overridepublic void onDrawFrame(GL10 gl) {init();// 初始化int x, y;float f_x, f_y, f_xb, f_yb; // 用來將旗形的波浪分割成很小的四邊形//gl.glClear(GL10.GL_DEPTH_BUFFER_BIT | GL10.GL_COLOR_BUFFER_BIT);gl.glLoadIdentity();//gl.glTranslatef(0.0f, 0.0f, -12.0f);// 往里縮12單位gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);// 繞X軸旋轉gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);// 繞Y軸旋轉gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);// 繞Z軸旋轉//gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);//gl.glVertexPointer(3, GL10.GL_FLOAT, 0, texBuffer);gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, coordBuffer);// gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);// 四邊形繪制開始for (x = 0; x < 44; x++) {for (y = 0; y < 44; y++) {f_x = (float) x / 44.0f; // 生成X的浮點數f_y = (float) y / 44.0f;// 生成Y的浮點數f_xb = (float) (x + 1) / 44.0f; // X浮點值+0.0227ff_yb = (float) (y + 1) / 44.0f;// Y浮點值+0.0227f//coordBuffer.clear();// 左下角coordBuffer.put(f_x);coordBuffer.put(f_y);// 左上角coordBuffer.put(f_x);coordBuffer.put(f_yb);// 右上角coordBuffer.put(f_xb);coordBuffer.put(f_yb);// 右下角coordBuffer.put(f_xb);coordBuffer.put(f_y);//// 左下角texBuffer.clear();texBuffer.put(vertex[x][y][0]);texBuffer.put(vertex[x][y][1]);texBuffer.put(vertex[x][y][2]);// 左上角texBuffer.put(vertex[x][y + 1][0]);texBuffer.put(vertex[x][y + 1][1]);texBuffer.put(vertex[x][y + 1][2]);// 右上角texBuffer.put(vertex[x + 1][y + 1][0]);texBuffer.put(vertex[x + 1][y + 1][1]);texBuffer.put(vertex[x + 1][y + 1][2]);// 右下角texBuffer.put(vertex[x + 1][y][0]);texBuffer.put(vertex[x + 1][y][1]);texBuffer.put(vertex[x + 1][y][2]);//注意:如果設為GL10.GL_TRIANGLE_STRIP模式的話,會產生黑點點,不好看gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);}}gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);/*** 每繪制兩次場景,循環一次sin值,以產生運動效果。 用來降低波浪速度(每隔2幀一次)*/if (wiggle_count == 2) { // 沿著Y 平面循環for (y = 0; y < 45; y++) {hold = vertex[0][y][2];for (x = 0; x < 44; x++) {// 當前波浪值等于其右側的波浪值vertex[x][y][2] = vertex[x + 1][y][2]; //}vertex[44][y][2] = hold; // 剛才的值成為最左側的波浪值}wiggle_count = 0;// 清零}wiggle_count++;// 加一xrot += 0.3f;yrot += 0.2f;zrot += 0.4f;}@Overridepublic void onSurfaceChanged(GL10 gl, int width, int height) {gl.glViewport(0, 0, width, height);float ratio = (float) width / height;gl.glMatrixMode(GL10.GL_PROJECTION);gl.glLoadIdentity();gl.glFrustumf(-ratio, ratio, -1, 1, 1, 15);// 設置觀察模型gl.glMatrixMode(GL10.GL_MODELVIEW);gl.glLoadIdentity();}@Overridepublic void onSurfaceCreated(GL10 gl, EGLConfig config) {gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);// 黑色背景色gl.glClearColorx(0, 0, 0, 0);// 啟用陰影平滑gl.glShadeModel(GL10.GL_SMOOTH);// 啟用深度測試gl.glEnable(GL10.GL_DEPTH_TEST);// 深度測試類型gl.glDepthFunc(GL10.GL_LEQUAL);// 設置深度緩存gl.glClearDepthf(1.0f);// 啟用紋理gl.glEnable(GL10.GL_TEXTURE_2D);// 生成紋理gl.glGenTextures(1, textures, 0);// 綁定紋理gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, initBitmap.bitmap, 0);//線性濾波gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_LINEAR);gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR); /*** */// 沿X平面循環for (int x = 0; x < 45; x++) {// 沿Y平面循環for (int y = 0; y < 45; y++) {// 向表面添加波浪效果vertex[x][y][0] = (float) ((x / 5.0f) - 4.5f);vertex[x][y][1] = (float) ((y / 5.0f) - 4.5f);// 2 *pai* r 角度vertex[x][y][2] = (float) (Math.sin(((((float) x / 5.0f) * 40.0f) / 360.0f) * 3.141592654 * 2.0f));}}}}4、運行效果:
總結
以上是生活随笔為你收集整理的Android实现飘动的旗帜效果实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opengl 相关资料
- 下一篇: (转)android四种动画