我的android GLSurfaceView.Renderer从不调用onSurafaceCreated()

时间:2016-01-06 19:40:25

标签: android opengl-es opengl-es-2.0 glsurfaceview

我正在尝试遵循给定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;
    }
}

这是主要的活动文件。

//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 个答案:

答案 0 :(得分:0)

如果您扩展GLSurfaceView,那么当您覆盖super的生命周期方法(例如surfaceview等)时,别忘了调用onSurfaceCreate