代码之家  ›  专栏  ›  技术社区  ›  gloo

我的android GLSurfaceView。渲染器从不调用SurafaceCreated()

  •  0
  • gloo  · 技术社区  · 9 年前

    我正在尝试遵循官方给出的android opengl es2教程 here 。然而,我的渲染器 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;
        }
    }
    

    这是主要的活动文件。

    //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;
            }
        }
    }
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   wdanxna    6 年前

    如果您延长 GLSurfaceView 那别忘了打电话 super 当您覆盖 surfaceview 的生命周期方法,如 onSurfaceCreate