问题在android中初始化GLview

时间:2011-01-07 07:28:09

标签: android

我编写了以下代码来初始化android中的GLview。我是android应用程序开发的新手。我想使用代码来获取屏幕截图。

以下是我编写的代码:

public class MainActivity extends Activity{

    /** Called when the activity is first created. */

    private EGL10 egl;
    private EGLDisplay display;
    private EGLConfig config;
    Display disp ;
    private EGLContext eglContext;
    private GL10 gl ;
    private EGLSurface surface;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // ToDo add your GUI initialization code here        
    setContentView(R.layout.main);

    disp = getWindowManager().getDefaultDisplay();

    egl = (EGL10) EGLContext.getEGL();
   display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
   int[] ver = new int[2];
   egl.eglInitialize(display, ver);
    int[] configSpec = {EGL10.EGL_NONE};
    EGLConfig[] configOut = new EGLConfig[1];
    int[] nConfig = new int[1];
    egl.eglChooseConfig(display, configSpec, configOut, 1, nConfig);
    config = configOut[0];
    eglContext = egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, null);

    SurfaceView view = new SurfaceView(this);
    setContentView(view); 

      SurfaceHolder holder = view.getHolder();
      holder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
      try{
            surface = egl.eglCreateWindowSurface(display,config, holder , null);
      }catch(Exception e)
      {
          e.printStackTrace();
      }
    egl.eglMakeCurrent(display, surface, surface, eglContext);
    gl = (GL10) eglContext.getGL();




    savePNG(0,0,disp.getWidth(), disp.getHeight(),"testpicture.png",gl);


    }

但是应用程序终止于

      try{
            surface = egl.eglCreateWindowSurface(display,config, holder , null);
      }catch(Exception e)
      {
          e.printStackTrace();
      }

正在提供例外

 IlligalArgument exception: Must use SurfaceHolder or SurfaceView  with proper format. not a valid surface.

请纠正这个问题。

谢谢。

1 个答案:

答案 0 :(得分:1)

GLSurfaceView类提供了一种使用OpenGL的简便方法。有一个教程here