首页 文章

在VideoView中定位视频

提问于
浏览
47

所以我扩展了VideoView的onMeasure以扩展视频以适应全屏视图 .

这是如何:

public void setVideoAspect(int w,int h){
    wVideo=w;
    hVideo=h;
    onMeasure(w, h);
}
 @Override
 protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
 {
     super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     if(wVideo!=0 && hVideo!=0)
     setMeasuredDimension(wVideo,hVideo);
 }

我使用屏幕的显示指标(宽度,高度)调用setVideoAspect() . 问题是这种方法会拉伸视频以适应屏幕内部 . 我希望能够保持纵横比 . (我有4:3的视频和3:2的屏幕尺寸 . )我使用下面的代码给视图保留比率测量值:

int height =  (int) (metrics.widthPixels*3/(float)4);
                        int width=  metrics.widthPixels;   
                        mVideoView.setVideoAspect(width,height);

所以这可以完成这项工作,但是有一个问题:它给了我一个4:3的视频,屏幕的宽度和正确的比例,但它不会使视频居中 . (它只是播放视频的底部而不是顶部和底部 . )我有一个包含VideoView的相对布局,VideoView的重力设置为居中 .

4 回答

  • 8

    请尝试使用 FrameLayout . 我不知道为什么,但是如果我在我的代码中使用 LinearRelative 它不会居中,但 FrameLayout 会 . 这是适合我的视频到屏幕的XML,保留比率并使其居中:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/bg">
        <!-- Video player -->
        <VideoView
            android:id="@+id/surface_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"/>
    </FrameLayout>
    
  • 13

    为了将视频集中在 RelativeLayout 中,我添加了 layout_gravity="center" ad layout_centerInParent="true" . 它适用于我的Android 4.3手机 .

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <VideoView android:id="@+id/surface_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_centerInParent="true" />
    </RelativeLayout>
    
  • 119

    Cameron以程序化的方式回答(如果像我这样的人需要它)这段代码在我的代码中的一个活动的创建内部('this'在下面指的是活动)

    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    
        FrameLayout fl = new FrameLayout(this);
    
        fl.setLayoutParams(lp);
    
        VideoView vv = new VideoView(this);
    
        FrameLayout.LayoutParams lp2 = new FrameLayout.LayoutParams(lp);
    
        lp2.gravity = Gravity.CENTER;
    
        vv.setLayoutParams(lp2);
    
        fl.addView(vv);
    
        setContentView(fl);
    
  • 0

    这适用于保持视频宽高比的任何视频 . 它将视频定位在VideoView内,并像ImageView一样执行Center Crop或Center Inside .

    我使用VideoView来覆盖整个ConstraintLayout . 您可以使用match_parent作为宽度和高度的任何其他布局 .

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        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_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>
    

    在onCreate:

    Uri uri = //The uri of your video.
    VideoView videoView = findViewById(R.id.videoView);
    videoView.setVideoURI(uri);
    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
    
            //Get your video's width and height
            int videoWidth = mp.getVideoWidth();
            int videoHeight = mp.getVideoHeight();
    
            //Get VideoView's current width and height
            int videoViewWidth = videoView.getWidth();
            int videoViewHeight = videoView.getHeight();
    
            float xScale = (float) videoViewWidth / videoWidth;
            float yScale = (float) videoViewHeight / videoHeight;
    
            //For Center Crop use the Math.max to calculate the scale
            //float scale = Math.max(xScale, yScale);
            //For Center Inside use the Math.min scale. 
            //I prefer Center Inside so I am using Math.min
            float scale = Math.min(xScale, yScale);
    
            float scaledWidth = scale * videoWidth;
            float scaledHeight = scale * videoHeight;
    
            //Set the new size for the VideoView based on the dimensions of the video
            ViewGroup.LayoutParams layoutParams = videoView.getLayoutParams();
            layoutParams.width = (int)scaledWidth;
            layoutParams.height = (int)scaledHeight;
            videoView.setLayoutParams(layoutParams);               
        }
     });
    

    希望它可以帮到某人!

相关问题