简体   繁体   English

每次方向更改时调用onSurfaceCreated()

[英]onSurfaceCreated() called every time orientation changes

I'm implementing the GLSurfaceView.Renderer like so: 我正在实现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()");
    }

}

This class is implemented in the MainActivity: 此类在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 is: 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());
    }
}

Very simple as you can see. 你可以看到非常简单。 When I now start the App, the onSurfaceCreated() method is correctly called, follow by one call of onSurfaceChanged(). 当我现在启动App时,正确调用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. 根据我的理解,只要需要创建新表面,就会调用onSurfaceCreated()方法。 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? 为了调整视口,仅触发onSurfaceChanged()调用是不是足够了?

Note that I don't put my device to sleep when changing the orientation. 请注意,更改方向时,我不会让设备进入睡眠状态。

DO this way 这样做

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

The one of advantages of OpenGL that you draw regards to screen size. 您绘制的OpenGL的优点之一是关于屏幕大小。 It gives you ability to handle all Android resolutions. 它使您能够处理所有Android分辨率。

I'm not sure how it works with GL20 (sure the same like GL10). 我不确定它如何与GL20 (确保像GL10一样)。

As I know in onSurfaceChanged provides several configurations for OpenGL based on length/width of your screen. 据我所知, onSurfaceChanged根据屏幕的长度/宽度为OpenGL提供了几种配置。

For example glViewport 例如glViewport

It is necessary to call glViewport handler when GL view dimensions are modified. 修改GL视图尺寸时,必须调用glViewport处理程序。

Only if you have width = height is unnecessary but its other story. 只有你有宽度=高度是不必要的,但它的另一个故事。

as exampe 作为例证

@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);

If you want to avoid that, add to Manifest.xml: 如果要避免这种情况,请添加到Manifest.xml:

<activity android:name="Activity"
  android:configChanges="screenSize|orientation">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM