简体   繁体   English

Android VideoView-更改屏幕方向后保持宽高比

[英]Android VideoView - maintaining aspect-fill after a screen orientation change

I'm an iOS developer porting an iOS app to Android, so bear with me.I have a VideoView that is acting as a "video background". 我是将iOS应用程序移植到Android的iOS开发人员,所以请耐心等待。我有一个VideoView充当“视频背景”。 Everything about it is working correctly - except for in portrait view and on orientation change (to portrait view). 关于它的所有内容均正常运行-除了在纵向视图和方向更改(纵向视图)上。 In the AndroidManifest file, I have my activity setup so that screen orientations do not trigger a layout refresh: 在AndroidManifest文件中,我进行了活动设置,因此屏幕方向不会触发布局刷新:

android:configChanges="orientation|screenSize"

This is working correctly; 这工作正常; what isn't working correctly is the aspect ratio of the video being played in the VideoView. 无法正常工作的是在VideoView中播放的视频的纵横比。 The video is skewed to fit within the portrait bounds instead of cropping out the left and right sides to maintain aspect ratio. 将视频倾斜以适合纵向范围,而不是裁剪左侧和右侧以保持宽高比。 How can I force the VideoView to "aspect-fill" the video in it's container? 如何强制VideoView在容器中“纵横填满”视频? I should also mention that this VideoView is a subview of a ConstraintView. 我还应该提到,此VideoView是ConstraintView的子视图。 Here is the applicable XML: 这是适用的XML:

<VideoView
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:id="@+id/videoView2"
    app:layout_constraintBottom_toBottomOf="@+id/imageView3"
    app:layout_constraintLeft_toLeftOf="@+id/imageView3"
    app:layout_constraintRight_toRightOf="@+id/imageView3"
    app:layout_constraintTop_toTopOf="@+id/imageView4"
    android:scaleType="centerCrop"
    android:adjustViewBounds="true"/>

You can check for the orientation programatically after the orientation change (through onConfigurationChanged() ) and set your videoView scale type accordingly to what you want. 您可以在方向更改后(通过onConfigurationChanged() )以编程方式检查方向,并根据需要设置videoView比例尺类型。

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
        // set video view configurations to what you want on landscape
    }else{
        // set video view configurations to what you want on portrait
    }
}

About scaling the video view, that's another issue. 关于缩放视频视图,这是另一个问题。 Check this answers for more info about it: https://stackoverflow.com/a/7225469/4244598 检查此答案以获取有关它的更多信息: https : //stackoverflow.com/a/7225469/4244598

https://stackoverflow.com/a/12335916/4244598 https://stackoverflow.com/a/12335916/4244598

http://blog.kasenlam.com/2012/02/android-how-to-stretch-video-to-fill.html http://blog.kasenlam.com/2012/02/android-how-to-stretch-video-to-fill.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM