Android:屏幕旋转会改变视频大小

时间:2017-04-25 20:10:40

标签: android video rotation

在我的片段中,我放了一段视频。我不想在旋转屏幕时重新启动活动,所以我添加了android:configChanges="orientation|screenSize"。但现在,当我旋转屏幕时,视频大小非常奇怪。见这些图片:

当我旋转到横向时: enter image description here 这是当我旋转到肖像时: enter image description here 只有在我的清单中有android:configChanges="orientation|screenSize"时才会发生这种情况,如果我将其删除,则不会发生。

这是我的视频布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<VideoView
    android:id="@+id/videoView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" />
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

当你添加android:configChanges =“orientation | screenSize”并避免重启时,系统会调用你活动的onConfigurationChanged()。

在这里,您可以处理对您的应用程序而言可能很重要的任何内容,例如为新方向调整视频视图的大小。

如果不这样做,系统将使用不再与显示匹配的旧视图尺寸。

有关如何调整视频视图大小的示例,请参阅此答案:https://stackoverflow.com/a/14113271/334402

Google的Camerea2Vdieo示例(https://github.com/googlesamples/android-Camera2Video)显示了一种在曲面发生变化时调整视频大小的方法 - 它们不会停止重新启动活动但原则相同,因为您检测到“窗口”已更改然后测量新的大小并相应地重置:

使用SurfaceTextureListener检测更改:

/**
     * {@link TextureView.SurfaceTextureListener} handles several lifecycle events on a
     * {@link TextureView}.
     */
    private TextureView.SurfaceTextureListener mSurfaceTextureListener
            = new TextureView.SurfaceTextureListener() {

        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture,
                                              int width, int height) {
            openCamera(width, height);
        }

        @Override
        public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture,
                                                int width, int height) {
            configureTransform(width, height);
        }

        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
            return true;
        }

        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
        }

    };

纹理的转换在configureTransform中完成,您可以在上面的链接中看到。

相关问题