4

我正在像这样实现 GLSurfaceView.Renderer:

public class GL20Renderer implements GLSurfaceView.Renderer {
    private static GL20Renderer mInstance = new GL20Renderer();
    private GL20Renderer() {}
    public static GL20Renderer getInstance() {
        return mInstance;
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        Log.e("App", "onDrawFrame()");
    }
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        Log.e("App", "onSurfaceChanged()");
    }
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        Log.e("App", "onSurfaceCreated()");
    }

}

这个类在 MainActivity 中实现:

public class MainActivity extends Activity {
    private GLSurfaceView mGLView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // Create a GLSurfaceView instance and set it as the ContentView for this Activity
        mGLView = new GL20SurfaceView(this);
        setContentView(mGLView);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mGLView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGLView.onResume();
    }

}

GL20SurfaceView 是:

public class GL20SurfaceView extends GLSurfaceView {
    public GL20SurfaceView(Context context) {
        super(context);

        // Create an OpenGL ES 2.0 context.
        setEGLContextClientVersion(2);

        // Set the Renderer for drawing on the GLSurfaceView
        setRenderer(GL20Renderer.getInstance());
    }
}

如您所见,非常简单。当我现在启动应用程序时,正确调用了 onSurfaceCreated() 方法,然后调用了一次 onSurfaceChanged()。

现在的问题是:每当设备方向发生变化时,我都会收到另一个 onSurfaceCreated() 调用,然后是 onSurfaceChanged()

据我了解,每当需要创建新表面时,都会调用 onSurfaceCreated() 方法。我的问题是:为什么每当我改变设备方向时它会这样做?仅触发 onSurfaceChanged() 调用以调整视口不就足够了吗?

请注意,更改方向时我不会让设备进入睡眠状态。

4

2 回答 2

4

这样做

<activity
            android:name=".MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            />
于 2013-09-28T13:02:35.003 回答
1

您绘制的 OpenGL 的优点之一是关于屏幕大小。它使您能够处理所有 Android 分辨率。

我不确定它是如何工作的GL20(肯定和 GL10 一样)。

据我所知,onSurfaceChanged根据屏幕的长度/宽度为 OpenGL 提供了几种配置。

例如glViewport

修改视图尺寸时需要调用glViewport处理程序。GL

只有当你有宽度 = 高度是不必要的,但它的另一个故事。

例如

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {

    // prevent 0 divide
    if(height == 0) {
        height=1;
    }

    screenWidth = width;
    screenHeight = height;
    ratio = (float) width/height;
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glOrthof(0, width, 0, height, -10f, 10f);
    gl.glViewport(0, 0, screenWidth, screenHeight);

如果您想避免这种情况,请添加到 Manifest.xml:

<activity android:name="Activity"
  android:configChanges="screenSize|orientation">
于 2013-09-28T13:02:38.823 回答