我的 android GLSurfaceView.Renderer 从不调用 onSurfaceCreated()
my android GLSurfaceView.Renderer never calls onSurafaceCreated()
我正在尝试按照 here 提供的官方 android opengl es2 教程进行操作。但是我的渲染器的 onSurfaceCreated
方法从未被调用。有什么建议吗?
//GLRenderer.java
package com.studio.test1;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.util.Log;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class GLRenderer implements GLSurfaceView.Renderer {
private Triangle triangle;
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
// Set the background frame color
Log.i("test","xxxxxxxx");
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
triangle = new Triangle();
}
public void onDrawFrame(GL10 unused) {
// Redraw background color
//GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
Log.i("open","xxxxxx");
triangle.draw();
}
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}
public static int loadShader(int type, String shaderCode){
// create a vertex shader type (GLES20.GL_VERTEX_SHADER)
// or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
int shader = GLES20.glCreateShader(type);
// add the source code to the shader and compile it
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}
}
这是主要的 activity 文件。
//MainActivity.java
package com.studio.test1;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
//import android.view.Menu;
import android.view.SurfaceView;
import android.content.Context;
import android.util.Log;
import android.graphics.*;
import com.studio.graphics.Graph;
import com.studio.graphics.Image;
import com.studio.menu.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SurfacePanel(this));
Log.i("hello","world");
}
public class SurfacePanel extends GLSurfaceView {
private int xmax;
private int ymax;
private final GLRenderer renderer;
public SurfacePanel(Context context) {
super(context);
Log.i("making", "panel");
setEGLContextClientVersion(2);
renderer = new GLRenderer();
setRenderer(renderer);
//setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder){
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.v("surface","created");
xmax = getWidth();
ymax = getHeight();
}
public boolean onTouchEvent(MotionEvent e){
int action = e.getActionMasked();
Point z=new Point(e.getX(),e.getY());
switch(action){
case MotionEvent.ACTION_DOWN:
TouchMaster.onDown(z);
SwipeMaster.down(z);
break;
case MotionEvent.ACTION_UP:
TouchMaster.onUp(z);
SwipeMaster.up(z);
break;
case MotionEvent.ACTION_MOVE:
TouchMaster.onMove(z);
SwipeMaster.move(z);
}
return true;
}
}
}
如果你扩展了GLSurfaceView
那么当你重写surfaceview
的生命周期方法如onSurfaceCreate
等时不要忘记调用super
我正在尝试按照 here 提供的官方 android opengl es2 教程进行操作。但是我的渲染器的 onSurfaceCreated
方法从未被调用。有什么建议吗?
//GLRenderer.java
package com.studio.test1;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.util.Log;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class GLRenderer implements GLSurfaceView.Renderer {
private Triangle triangle;
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
// Set the background frame color
Log.i("test","xxxxxxxx");
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
triangle = new Triangle();
}
public void onDrawFrame(GL10 unused) {
// Redraw background color
//GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
Log.i("open","xxxxxx");
triangle.draw();
}
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}
public static int loadShader(int type, String shaderCode){
// create a vertex shader type (GLES20.GL_VERTEX_SHADER)
// or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
int shader = GLES20.glCreateShader(type);
// add the source code to the shader and compile it
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}
}
这是主要的 activity 文件。
//MainActivity.java
package com.studio.test1;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
//import android.view.Menu;
import android.view.SurfaceView;
import android.content.Context;
import android.util.Log;
import android.graphics.*;
import com.studio.graphics.Graph;
import com.studio.graphics.Image;
import com.studio.menu.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SurfacePanel(this));
Log.i("hello","world");
}
public class SurfacePanel extends GLSurfaceView {
private int xmax;
private int ymax;
private final GLRenderer renderer;
public SurfacePanel(Context context) {
super(context);
Log.i("making", "panel");
setEGLContextClientVersion(2);
renderer = new GLRenderer();
setRenderer(renderer);
//setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder){
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.v("surface","created");
xmax = getWidth();
ymax = getHeight();
}
public boolean onTouchEvent(MotionEvent e){
int action = e.getActionMasked();
Point z=new Point(e.getX(),e.getY());
switch(action){
case MotionEvent.ACTION_DOWN:
TouchMaster.onDown(z);
SwipeMaster.down(z);
break;
case MotionEvent.ACTION_UP:
TouchMaster.onUp(z);
SwipeMaster.up(z);
break;
case MotionEvent.ACTION_MOVE:
TouchMaster.onMove(z);
SwipeMaster.move(z);
}
return true;
}
}
}
如果你扩展了GLSurfaceView
那么当你重写surfaceview
的生命周期方法如onSurfaceCreate
等时不要忘记调用super