【问题标题】:How to make video full screen and keep aspect ratio?如何使视频全屏并保持纵横比?
【发布时间】:2015-07-30 12:23:55
【问题描述】:

我有一个单一尺寸的视频,无论屏幕大小如何,我都希望它始终显示全尺寸。同时我想保持该视频的纵横比,这样它就不会拉伸,我会造成不好的外观。

我可以实现这样的拉伸效果,但视频看起来很糟糕:

<?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 有什么作物替代品吗?谢谢你:)

【问题讨论】:

标签: android android-videoview


【解决方案1】:

你可以尝试使用这个库

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

【讨论】:

  • 超级完美@JoaoBiriba,我希望我可以多次投票。
【解决方案2】:

我知道这是一个老问题。但以防万一这有助于某人。 就我而言,我使用的是扩展 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 的原因,如果您想知道的话.. ;)

【讨论】:

  • 谢谢,这正是我想要的。非常感谢.. 尝试过可能的 Lib,但这段代码对我有帮助。
【解决方案3】:

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

    app:layout_constraintDimensionRatio="16:9"

【讨论】:

    【解决方案4】:

    我需要在启动画面中播放垂直视频。由于长宽比问题,它无法在所有屏幕上播放,也不适合全屏。

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

    【讨论】:

      【解决方案5】:

      像这样你可以自己设置视频的属性。使用 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-13
        • 1970-01-01
        • 1970-01-01
        • 2013-12-29
        • 1970-01-01
        • 2017-08-31
        • 1970-01-01
        • 2021-05-12
        相关资源
        最近更新 更多