每次方向变化时调用onSurfaceCreated() [英] onSurfaceCreated() called every time orientation changes

查看:504
本文介绍了每次方向变化时调用onSurfaceCreated()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm implementing the GLSurfaceView.Renderer like so:

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中实现:

This class is implemented in the 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是:

GL20SurfaceView is:

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().

Very simple as you can see. When I now start the App, the onSurfaceCreated() method is correctly called, follow by one call of onSurfaceChanged().

现在的问题是:每当设备方向发生变化时,我都会先调用onSurfaceCreated(),再调用onSurfaceChanged().

Problem now is: Whenever the device orientation changes, I get another call of onSurfaceCreated() followed by onSurfaceChanged().

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

In my understanding, the onSurfaceCreated() method is called whenever a new surface needs to be created. My question is: Why does it do that whenever I change just the device orientation? Shouldn't it be sufficient that only a onSurfaceChanged() call is triggered in order to adjust the viewport?

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

Note that I don't put my device to sleep when changing the orientation.

推荐答案

以这种方式执行

<activity
            android:name=".MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            />

这篇关于每次方向变化时调用onSurfaceCreated()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆