日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android实现飘动的旗帜效果实例

發布時間:2025/3/15 Android 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android实现飘动的旗帜效果实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


做一個飄動的旗幟效果,思路大概為:將旗形的波浪分割成很小的四邊形,然后多少時間進行刷新,Z方向上運用sin函數改變,看起來有飄動的感覺


實例代碼如下:

1、準備圖片資源 : initBitmap類

import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory;public class initBitmap {public static Bitmap bitmap;public static void init(Resources res){bitmap = BitmapFactory.decodeResource(res, R.drawable.flag) ; //圖片} }
2、Activity類


import android.app.Activity; import android.opengl.GLSurfaceView; import android.os.Bundle;public class FlagOpenGLActivity extends Activity {GLSurfaceView glView;FlagRender flagRender = new FlagRender();@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);initBitmap.init(this.getResources());glView = new GLSurfaceView(this);glView.setRenderer(flagRender);setContentView(glView);} }

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实现飘动的旗帜效果实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。