Android VideoView以匹配父级高度并保持纵横比

Android VideoView以匹配父级高度并保持纵横比,android,android-videoview,dimensions,aspect-ratio,Android,Android Videoview,Dimensions,Aspect Ratio,我有一个视频视图,设置如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/player" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <VideoVi

我有一个视频视图,设置如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/player" 
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

    <VideoView
        android:id="@+id/video"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true" />

    <ProgressBar
        android:id="@+id/loader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>
MediaController controllers = new MediaController(this) {
    @Override
    public void hide() {
        if (state != State.Hidden) {
            this.show();
        }
        else {
            super.hide();
        }
    }
};
controllers.setAnchorView(videoView);
videoView.setMediaController(controllers);

videoView.setOnPreparedListener(new OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        controllers.show();
    }
});
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/video_holder"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:clipToPadding="false">

      <VideoView  
          android:id="@+id/videoView"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_alignParentBottom="true"
          android:layout_alignParentEnd="true"
          android:layout_alignParentStart="true"
          android:layout_alignParentTop="true" />
</RelativeLayout>
    int i = videoView.getHeight() > videoView.getWidth() ? videoView.getHeight() : videoView.getWidth();
    video_holder.setLayoutParams(new ConstraintLayout.LayoutParams(i, i));
这非常有效,控制器始终保持打开状态,但在计算视频放置位置时,没有考虑控制器的高度(因为它是垂直居中的)

我的两个问题是:

  • 如何使视频视图与父视图的高度匹配,同时保持纵横比
  • 如何使视频视图考虑其控制器的高度

  • 谢谢。

    您应该扩展内置视频视图

    在显示视频视图之前,请调用
    setVideoSize
    ,您可以从视频中提取的缩略图中获取视频大小

    因此,当调用视频视图的
    onMeasure
    时,
    mvideowith
    mvideowith
    都大于0

    如果您想计算控制器的高度,您可以在
    onMeasure
    方法中自己计算

    希望会有所帮助

    public class MyVideoView extends VideoView {
    
            private int mVideoWidth;
            private int mVideoHeight;
    
            public MyVideoView(Context context, AttributeSet attrs) {
                super(context, attrs);
            }
    
            public MyVideoView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
            }
    
            public MyVideoView(Context context) {
                super(context);
            }
    
            public void setVideoSize(int width, int height) {
                mVideoWidth = width;
                mVideoHeight = height;
            }
    
            @Override
            protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                // Log.i("@@@", "onMeasure");
                int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
                int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
                if (mVideoWidth > 0 && mVideoHeight > 0) {
                    if (mVideoWidth * height > width * mVideoHeight) {
                        // Log.i("@@@", "image too tall, correcting");
                        height = width * mVideoHeight / mVideoWidth;
                    } else if (mVideoWidth * height < width * mVideoHeight) {
                        // Log.i("@@@", "image too wide, correcting");
                        width = height * mVideoWidth / mVideoHeight;
                    } else {
                        // Log.i("@@@", "aspect ratio is correct: " +
                        // width+"/"+height+"="+
                        // mVideoWidth+"/"+mVideoHeight);
                    }
                }
                // Log.i("@@@", "setting size: " + width + 'x' + height);
                setMeasuredDimension(width, height);
            }
    }
    
    公共类MyVideoView扩展了VideoView{
    私有整数宽度;
    私人身高;
    公共MyVideoView(上下文、属性集属性){
    超级(上下文,attrs);
    }
    公共MyVideoView(上下文、属性集属性、int-defStyle){
    超级(上下文、属性、定义样式);
    }
    公共MyVideoView(上下文){
    超级(上下文);
    }
    公共空间设置VideoSize(整数宽度、整数高度){
    mVideoWidth=宽度;
    mVideoHeight=高度;
    }
    @凌驾
    测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
    //Log.i(“@@@1”,“测量时”);
    int-width=getDefaultSize(MVideWidth,widthMeasureSpec);
    int height=getDefaultSize(MVideHeight、heightMeasureSpec);
    如果(mvideowitch>0&&mvideowitch>0){
    如果(MVideWidth*height>width*MVideHeight){
    //Log.i(“@@@@1”,“图像太高,正在更正”);
    高度=宽度*mVideoHeight/mVideoWidth;
    }else if(MVideWidth*height
    我用布局解决了这个问题。当它被固定在角落时,它似乎工作得很好,但它导致了视频倾斜。为了进行测试,我将相对布局的背景更改为#990000,以便看到红色的背景

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/relative_parent"
        android:background="#000000">
        <VideoView
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:id="@+id/videoView" />
    </RelativeLayout>
    
    
    
    公共类MyVideoView扩展了VideoView{
    私有整数宽度;
    私人身高;
    公共MyVideoView(上下文、属性集属性){
    超级(上下文,attrs);
    }
    公共MyVideoView(上下文、属性集属性、int-defStyle){
    超级(上下文、属性、定义样式);
    }
    公共MyVideoView(上下文){
    超级(上下文);
    }
    @凌驾
    公共void setVideoURI(Uri){
    MediaMetadataRetriever retriever=新的MediaMetadataRetriever();
    retriever.setDataSource(this.getContext(),uri);
    mVideoWidth=Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA\u KEY\u VIDEO\u WIDTH));
    mVideoHeight=Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA\u KEY\u VIDEO\u HEIGHT));
    super.setVideoURI(uri);
    }
    @凌驾
    测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
    //Log.i(“@@@1”,“测量时”);
    int-width=getDefaultSize(MVideWidth,widthMeasureSpec);
    int height=getDefaultSize(MVideHeight、heightMeasureSpec);
    如果(mvideowitch>0&&mvideowitch>0){
    如果(MVideWidth*height>width*MVideHeight){
    //Log.i(“@@@@1”,“图像太高,正在更正”);
    高度=宽度*mVideoHeight/mVideoWidth;
    }else if(MVideWidth*height
    关于问题1,我很惊讶没有人提到可能使用MediaPlayer的缩放模式。

    它有两种模式。它们都始终填充视图区域。要使其在保留纵横比的同时填充空间,从而裁剪长边,需要切换到第二种模式,
    VIDEO\u SCALING\u mode\u SCALE\u To\u FIT\u WITH\u croping
    。这解决了问题的一部分。另一部分是改变VideoView的测量行为,正如其他一些答案所示。我就是这样做的,主要是出于懒惰和不熟悉其他人使用的元数据API,欢迎您使用此方法或其他方法之一来修复视图的大小。当在层存在之前调用时,毯子捕获确保了安全性,因为它可能被多次调用,并且如果字段名发生更改,它也会返回到旧行为

    class FixedSizeVideoView : VideoView {
        constructor(ctx: Context) : super(ctx)
        constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs)
    
        // rather than shrink down to fit, stay at the size requested by layout params. Let the scaling mode
        // of the media player shine through. If the scaling mode on the media player is set to the one
        // with cropping, you can make a player similar to AVLayerVideoGravityResizeAspectFill on iOS
        override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
            try {
                val mpField = VideoView::class.java.getDeclaredField("mMediaPlayer")
                mpField.isAccessible = true
                val mediaPlayer: MediaPlayer = mpField.get(this) as MediaPlayer
    
                val width = View.getDefaultSize(mediaPlayer.videoWidth, widthMeasureSpec)
                val height = View.getDefaultSize(mediaPlayer.videoHeight, heightMeasureSpec)
                setMeasuredDimension(width, height)
            }
            catch (ex: Exception) {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec)
            }
        }
    }
    
    因此,在布局中使用这个类,只要在有机会的情况下更改媒体播放器上的缩放模式即可。例如:

            video.setOnPreparedListener { mp: MediaPlayer ->
                mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING)
                mp.isLooping = true
                mp.setScreenOnWhilePlaying(false)
            }
            video.start()
    

    我尝试了很多解决方案,而我的视频总是1000*1000格式,所以我为了解自己方面的人创建了一个简单的解决方案
        int i = videoView.getHeight() > videoView.getWidth() ? videoView.getHeight() : videoView.getWidth();
        video_holder.setLayoutParams(new ConstraintLayout.LayoutParams(i, i));
    
    val max = if (videoView.height > videoView.width) videoView.height else videoView.width
    videoView.layoutParams = ConstraintLayout.LayoutParams(max, max)
    
    <VideoView
         android:id="@+id/video"
         android:layout_width="2000dp"
         android:layout_height="match_parent" />
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
        // getting screen size
        ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int width = displayMetrics.widthPixels;
        int height = displayMetrics.heightPixels;
    
        double videoSizeRatio = (double) mVideoHeight / mVideoWidth;
        double screenSizeRatio = (double) height / width;
    
        if (mVideoWidth > 0 && mVideoHeight > 0) {
            if (videoSizeRatio > screenSizeRatio) { // screen is wider than video width
                height = (int) (videoSizeRatio * width);
            } else if (videoSizeRatio < screenSizeRatio) {
                width = (int) (height / videoSizeRatio);
            }
    
        }
        setMeasuredDimension(width, height);
    }
    
     <YourCustomizedVideoView
        android:id="@+id/videoView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        />
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp">
    
        <VideoView
            android:id="@+id/videoView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true" />
    
    </RelativeLayout>
    
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <VideoView
            android:id="@+id/videoView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="3:4"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>