第一步:
繼承GLSurfaceView
第二步:
實作介面GLSurfaceView.Renderer{
void onSurfaceCreated(GL10 gl, EGLConfig config);
void onSurfaceChanged(GL10 gl, int width, int height);
void onDrawFrame(GL10 gl);}
第三步:
撰寫glsl腳本(render)
新建 WlGLSurfaceView extends GLSurfaceView
package com.example.opengldemo;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
public class WlGLSurfaceView extends GLSurfaceView {
public WlGLSurfaceView(Context context) {
this(context, null);
}
public WlGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
//初始化
//設定OpenGL版本
setEGLContextClientVersion(2);
//設定Renderer
setRenderer(new WlRender());
}
}
新建 WlRender implements GLSurfaceView.Renderer
package com.example.opengldemo;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class WlRender implements GLSurfaceView.Renderer {
@Override
public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
}
@Override
public void onSurfaceChanged(GL10 gl10, int i, int i1) {
//設定長寬高
GLES20.glViewport(0, 0, i, i1);
}
@Override
public void onDrawFrame(GL10 gl10) {
//清屏
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
//使用顏色清屏
GLES20.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
}
}
xml中呼叫
<com.example.opengldemo.WlGLSurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/210044.html
標籤:java
