現在,我把它簡化, 不要立方體. 只通過 OpenGL ES 的 glClearColor() 和 glClear() 指令, 實現一個簡單的隨機顏色 GLSurfaceView.
關於 OpenGL ES 指令的詳細描述, 請瀏覽: http://www.khronos.org/
從這個例子中, 可以知道,我們主要的工作是在 MyRenderer 類的 onDrawFrame() 回調函數中.
關於 android.opengl.GLSurfaceView.Renderer:
它是一個通用的渲染器接口, 負責調用 OpenGL. 詳細可參考 Android Developers Reference, GLSurfaceView.Renderer.
package com.AndroidGLSurfaceViewActivity;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
public class AndroidGLSurfaceViewActivity extends Activity {
private GLSurfaceView mGLSurfaceView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create our Preview view and set it as the content of our
// Activity
mGLSurfaceView = new GLSurfaceView(this);
mGLSurfaceView.setRenderer(new MyRenderer());
setContentView(mGLSurfaceView);
}
@Override
protected void onResume() {
super.onResume();
mGLSurfaceView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mGLSurfaceView.onPause();
}
}
package com.AndroidGLSurfaceViewActivity;
import java.util.Random;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class MyRenderer implements android.opengl.GLSurfaceView.Renderer{
Random random = new Random();
@Override
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
gl.glClearColor(
random.nextFloat(), //Red
random.nextFloat(), //Green
random.nextFloat(), //Blue
1); //Alpha
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
}
@Override
public void onSurfaceChanged(GL10 arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
// TODO Auto-generated method stub
}
}
相關文章:
- 創建自定義的 GLSurfaceView,以處理用戶輸入
超酷
回覆刪除