如何使视频全屏并保持纵横比?

6
我有一个视频,想要让它在任何屏幕大小下都能以全尺寸显示。同时,我希望保持视频的宽高比,以免拉伸导致不美观。
我可以通过拉伸来实现全尺寸显示,但这样会使视频显得很糟糕。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <VideoView android:id="@+id/videoViewRelative"
         android:layout_alignParentTop="true"
         android:layout_alignParentBottom="true"
         android:layout_alignParentLeft="true"
         android:layout_alignParentRight="true"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
    </VideoView>

</RelativeLayout>

在我的情况下,向用户展示整个视频并不重要,因此我需要像ImageView一样的东西:
android:scaleType="centerCrop"

有没有适用于VideoView的任何作物替代品?谢谢 :)

最好使用一些库,可以查看这个答案 http://stackoverflow.com/a/38971707/1153703 - Bikesh M
5个回答

4

1
超级完美 @JoaoBiriba,我希望我可以多次点赞。 - Ravi Vaniya

3
我是一位有用的助手,可以为您翻译文本。

我知道这是一个老问题。但是以防万一,这可能会对某些人有所帮助。

在我的情况下,我正在使用扩展了TextureView的CustomTextureVideoView。

而我正在将视图全屏显示。

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
    ((Activity)getContext()).getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
    int screenHeight = metrics.heightPixels;
    int screenWidth = (screenHeight*9)/16;
    setMeasuredDimension(screenWidth, screenHeight);
}

我正在使用DisplayMetrics获取显示器的高度,并调整视图的宽度,以保持视频的长宽比。我希望我的视频保持16:9的长宽比。

在布局中,使视图

android:layout_centerHorizontal="true"

这样两侧的切割就会从两个方向平均进行。

我希望我的视频可以自动适应三星S8的新潮宽高比18.5:9,这也是为什么纵横比是16:9的原因,如果你感到疑惑的话.. ;)


谢谢,这正是我想要的。非常感谢。我尝试了很多库,但这段代码帮了我很大的忙。 - Yog Guru

1

我需要在启动界面播放竖屏视频。由于长宽比问题,在所有屏幕上都无法正常播放,并且不能适应全屏。

通过使用@free_style发布的代码,这里是自定义视频视图,可使视频充满整个屏幕。它提供了我的问题的解决方案。也许对其他人也有所帮助。

请使用此类代替默认视频视图。

   public class KSplashScreenVideoView extends VideoView {
    public KSplashScreenVideoView(Context context) {
        super(context);
    }

    public KSplashScreenVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public KSplashScreenVideoView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        ((Activity) getContext()).getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
        int screenHeight = metrics.heightPixels;
        int screenWidth = (screenHeight * 9) / 16;
        setMeasuredDimension(screenWidth, screenHeight);

    }
}

1
使用最新的ConstraintLayout更容易解决。将您的布局转换为ConstraintLayout,然后添加videoView的代码行。
    app:layout_constraintDimensionRatio="16:9"

0

像这样,您可以自己设置视频的属性。使用SurfaceView(使您对视图有更多控制),将其设置为fill_parent以匹配整个屏幕。

尝试这段代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
              android:orientation="vertical" 
              android:layout_width="match_parent"
              android:layout_height="fill_parent">

    <SurfaceView
        android:id="@+id/surfaceViewFrame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center" >
    </SurfaceView>

</Linearlayout>

然后在你的Java代码中获取SurfaceView并将你的媒体播放器添加到其中

surfaceViewFrame = (SurfaceView) findViewById(R.id.surfaceViewFrame);
player = new MediaPlayer();
player.setDisplay(holder);

点击此链接查看详情


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接