Android Studio 使视频宽度相对于视频高度

Android Studio make video width relative to the video height

我正在尝试将背景制作成视频,但要获得正确的纵横比,我需要将高度设为 match_parent,然后将宽度设为相对于高度的比例。有什么办法吗?

到目前为止,我唯一的方法是手动设置宽度,但这意味着随着高度的变化,它在某些设备上看起来会被压扁或拉伸。

<VideoView
    android:id="@+id/vv_menu_card_animation"
    android:layout_width="1280dp"
    android:layout_height="match_parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

在 ConstrainLayout 中,您还可以通过 layout_constraintDimensionRatio 将小部件的一个维度定义为另一个维度的比率。您可以在这里阅读更多内容:ConstrainLayout: Ratio.

如果你想要 width = height,你可以将这行添加到你的 VideoView:

app:layout_constraintDimensionRatio="1:1" android:layout_width="0dp"

<VideoView
    android:id="@+id/vv_menu_card_animation"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintDimensionRatio="1:1"
 />