[英]onSurfaceCreated() called every time orientation changes
我正在实现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());
}
}
你可以看到非常简单。 当我现在启动App时,正确调用onSurfaceCreated()方法,然后调用onSurfaceChanged()。
现在的问题是:每当设备方向改变时, 我会再次调用onSurfaceCreated(),然后调用onSurfaceChanged() 。
根据我的理解,只要需要创建新表面,就会调用onSurfaceCreated()方法。 我的问题是:每当我改变设备方向时,为什么会这样做? 为了调整视口,仅触发onSurfaceChanged()调用是不是足够了?
请注意,更改方向时,我不会让设备进入睡眠状态。
这样做
<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
/>
您绘制的OpenGL的优点之一是关于屏幕大小。 它使您能够处理所有Android分辨率。
我不确定它如何与GL20
(确保像GL10一样)。
据我所知, onSurfaceChanged
根据屏幕的长度/宽度为OpenGL提供了几种配置。
例如glViewport
修改GL
视图尺寸时,必须调用glViewport
处理程序。
只有你有宽度=高度是不必要的,但它的另一个故事。
作为例证
@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">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.