[英]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.