Android 更改 videoview 框架布局内的大小

Android change videoview size inside the framelayout

我有一个框架布局,它有一个图像视图和 videoview,videoview 在图像视图的顶部。

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_above="@+id/linearLayout"
    android:layout_centerHorizontal="true">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imgFrameId"
        android:background="@android:drawable/sym_def_app_icon"
        android:layout_centerHorizontal="true" />

    <VideoView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/vidFrameId"/>

</FrameLayout>

它们大小相同,所以我的 videoview 覆盖了我的图像视图,但我不希望这样。如何将 videoview 的尺寸缩小到 50%(宽度和高度的 50%)?我尝试了很多方法,但 none 奏效了,大多数都会将尺寸缩小到一个比例,我希望它能完美缩小,比如如果原来是 500dp,那就是 250dp。

基于 these answers,您应该在自定义 class 中扩展 VideoView class,并在 onMeasure 方法中进行更改。

就代码而言,这将如下所示:

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 int width = widthMeasureSpec/2 ;
 int height = heightMeasureSpec/2 ;

 setMeasuredDimension(width, height);
 }

如果您需要更多帮助,请告诉我。