如何在改变设备方向时使用 SurfaceView 和 OpenGL 渲染时避免闪烁?
前提是我们必须让Activity跟随系统方向旋转,所以我在AndroidManifest.xml中添加以下代码:
android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenLayout|screenSize|uiMode"
android:screenOrientation="sensor"
并将旋转动画设置为 ROTATION_ANIMATION_SEAMLESS。
WindowManager.LayoutParams windowAttributes = getWindow().getAttributes(); windowAttributes.rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_SEAMLESS; getWindow().setAttributes(windowAttributes);
我在surfaceChanged()中重新设置了正确的glViewport、投影矩阵和模型矩阵,问题是改变设备方向时有些帧看起来很奇怪,但是旋转完成后,所有的帧都消失了。框架看起来不错。 这张图片显示了正在发生的事情:
它尝试在调用 onConfigurationChanged() 时重置视口和矩阵,但它不起作用。 我希望在改变设备方向时框架看起来无缝,就像 Pixels 的相机应用程序那样。
The precondition is we have to let Activity rotate follow the system orientation, so i add following code to the AndroidManifest.xml:
android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenLayout|screenSize|uiMode"
android:screenOrientation="sensor"
And set the rotationAnimation to ROTATION_ANIMATION_SEAMLESS.
WindowManager.LayoutParams windowAttributes = getWindow().getAttributes(); windowAttributes.rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_SEAMLESS; getWindow().setAttributes(windowAttributes);
I reset the correct glViewport, projection matrix and model matrix in surfaceChanged(),the proplem is that serveral frames looks weired while change the device orientation, but after rotation is complete, all frames look good.
This image shows what's going on:
It tried to reset viewport and matrix while onConfigurationChanged() is invoked, but it doesn't work.
I want the frames look seamless while change the device orientation, just as the Pixels' camera APP did.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很简单:不可能完全避免这种情况。
您可以在旋转未完成时让 SurfaceView 不可见,但 Surface 无法渲染尚不存在的像素来填充“横向模式”刚启动时创建的新空间:Surface/View 应该适应自身,并且需要几毫秒,即您报告的几帧(设备速度慢/便宜,更多帧受此行为/问题影响),特别是相机从不同方向获取新像素。
It's simple: it's not possibile to avoid this completly.
You could let the SurfaceView invisible while rotation is not completed, but the Surface cannot renders not-yet-existent pixels for fill the new space that is created when the "landscape mode" is just started: Surface/View should adapts itself and it takes few milliseconds which are few frames as you reported (more the Device is slow/cheap and more frames are affected by this behavious/issue) expecially for the Camera to get new pixels from a different orientation.