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

时间:2013-09-28 12:42:45

标签: android opengl-es orientation

我正在实现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()调用是不是足够了?

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

2 个答案:

答案 0 :(得分:4)

这样做

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

答案 1 :(得分:1)

您绘制的OpenGL的优势之一与屏幕尺寸有关。它使您能够处理所有Android分辨率。

我不确定它如何与GL20一起使用(确定与GL10相同)。

据我所知,onSurfaceChanged根据屏幕的长度/宽度为OpenGL提供了几种配置。

例如glViewport

在修改glViewport视图维度时,有必要调用GL处理程序。

只有你有宽度=高度是不必要的,但它的另一个故事。

作为示例

@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">
相关问题