如何制作全屏视频并保持宽高比?

时间:2015-05-18 20:56:14

标签: android android-videoview

我有一个尺寸的视频,无论屏幕的大小如何,都希望它能一直显示全尺寸。与此同时,我希望保持该视频的宽高比,这样它就不会拉伸,造成不好看。

我可以像这样完成拉伸效果,但视频看起来很糟糕:

<?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是否有替代作物?谢谢:))

5 个答案:

答案 0 :(得分:3)

我知道这是一个古老的问题。但是,以防这有助于某人。 在我的情况下,我使用的是CustomTextureVideoView,它扩展了TextureView

我正在

中全屏观看
@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"

这样两侧的两侧都会被平分。

我想让我的视频根据新的花式宽高比18.5:9的三星s8 进行自我调整,这就是为什么长宽比为16:9,以防你想知道...;)< / p>

答案 1 :(得分:1)

您可以尝试使用此库

https://github.com/dmytrodanylyk/video-crop

答案 2 :(得分:1)

像这样你可以自己设置视频的属性。使用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代码上获取表面视图并将媒体播放器添加到它

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

<强> for detail use this link

答案 3 :(得分:0)

使用最新的ConstraintLayout更容易解决。将您的布​​局转换为ConstraintLayout。然后添加videoView的行。

    app:layout_constraintDimensionRatio="16:9"

答案 4 :(得分:0)

我需要在初始屏幕中播放垂直视频。由于长宽比问题,它不能在所有屏幕上正常播放,也不适合全屏显示。

通过使用@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);

    }
}
相关问题