onSurfaceCreated()被称为每次方向更改 [英] onSurfaceCreated() called everytime orientation changes
问题描述
我实现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屋!