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.app.Activity;import android.opengl.GLSurfaceView;import android.opengl.GLU;import android.os.Bundle;import android.util.Log;public class MainActivity extends Activity {private final String TAG = "MainActivity";private GLSurfaceView glSurefaceView;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);glSurefaceView = new GLSurfaceView(this);glSurefaceView.setRenderer(new GLSurfaceViewRender());this.setContentView(glSurefaceView);}@Overrideprotected void onResume() {super.onResume();glSurefaceView.onResume();}@Overrideprotected void onPause() {super.onStop();glSurefaceView.onPause();}class GLSurfaceViewRender implements GLSurfaceView.Renderer {@Overridepublic void onSurfaceCreated(GL10 gl, EGLConfig config) {Log.i(TAG, "onSurfaceCreated");// 设置背景颜色gl.glClearColor(0.0f, 0f, 0f, 0.5f);// 启用顶点数组(否则glDrawArrays不起作用)gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);}@Overridepublic void onSurfaceChanged(GL10 gl, int width, int height) {Log.i(TAG, "onSurfaceChanged");// 设置输出屏幕大小gl.glViewport(0, 0, width, height);}private float[] mTriangleArray = {// X, Y, Z 这是一个等边三角形-0.5f, -0.25f, 0, 0.5f, -0.25f, 0, 0.0f, 0.559016994f, 0 };@Overridepublic void onDrawFrame(GL10 gl) {Log.i(TAG, "onDrawFrame");// 清除屏幕和深度缓存(如果不调用该代码, 将不显示glClearColor设置的颜色)// 同样如果将该代码放到 onSurfaceCreated 中屏幕会一直闪动gl.glClear(GL10.GL_COLOR_BUFFER_BIT);gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);FloatBuffer mTriangleBuffer = BufferUtil.floatToBuffer(mTriangleArray);gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mTriangleBuffer);gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);//gl.glLoadIdentity();// 平移 (矩阵相乘)gl.glTranslatef(-0.2f, 0.3f, 0f);}}static class BufferUtil {public static FloatBuffer mBuffer;public static FloatBuffer floatToBuffer(float[] a) {// 先初始化buffer,数组的长度*4,因为一个float占4个字节ByteBuffer mbb = ByteBuffer.allocateDirect(a.length * 4);// 数组排序用nativeOrdermbb.order(ByteOrder.nativeOrder());mBuffer = mbb.asFloatBuffer();mBuffer.put(a);mBuffer.position(0);return mBuffer;}}}

更多相关文章

  1. 设置背景颜色
  2. Android(安卓)屏蔽Home键
  3. Android设置手机音量
  4. Android屏幕旋转和Configuration的使用
  5. android 之 3G/gprs
  6. 跳转到Android网络设置
  7. GLSurfaceView使用-入门之HelloWorld--画背景图
  8. Android(安卓)Studio下 SVN忽略文件设置
  9. android 原生GET和POST提交数据

随机推荐

  1. 【Android】 Painless Thread
  2. 安卓手机恶意代码——Samsapo
  3. 《第一行代码--Android》读书笔记之内容
  4. Android MediaPlayer音频播放器详解
  5. 实现对特定内容改变字体颜色的TextView
  6. android自定义按钮控件样式
  7. ubuntu下eclipse Android(安卓)ADT中SDK
  8. 回忆录——国庆
  9. Android 基础知识介绍
  10. Android 通过代码实现控制数据网络的开关