安卓VideoView居中裁剪

15

我有一个 RelativeLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:foregroundGravity="center"
    android:gravity="center"
    android:orientation="horizontal" >

    <VideoView
        android:id="@+id/videoViewPanel"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center" 
        android:layout_centerInParent="true"/>

</RelativeLayout>

我需要显示视频全屏并进行剪裁。如果可以用ImageView做比较,我需要像crop_center一样展示。

我该如何使VideoView不自动调整视频大小以适合中心位置,而是进行居中裁剪?


这个VideoView的解决方案可以像ImageView一样保持视频的宽高比并进行居中裁剪或居中适应缩放。希望能对某些人有所帮助! https://dev59.com/6W445IYBdhLWcg3w7urD#53641686 - Carlitos
@Carlitos 它不会居中裁剪视频,因为它会改变VideoView的大小。 - Vladyslav Matviienko
6个回答

30
在Android的VideoView中,有一种简单易行的方法可以实现与ImageView.ScaleType.CENTER_CROP相同的效果。
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="@dimen/dimen_0dp"
        android:layout_height="@dimen/dimen_0dp"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

在JAVA中:

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
   @Override
   public void onPrepared(MediaPlayer mp) {
         float videoRatio = mp.getVideoWidth() / (float) mp.getVideoHeight();
         float screenRatio = videoView.getWidth() / (float) 
         videoView.getHeight();
         float scaleX = videoRatio / screenRatio;
         if (scaleX >= 1f) {
             videoView.setScaleX(scaleX);
         } else {
             videoView.setScaleY(1f / scaleX);
         }
      }
});
在 Kotlin 中:
videoView.setOnPreparedListener { mediaPlayer ->
    val videoRatio = mediaPlayer.videoWidth / mediaPlayer.videoHeight.toFloat()
    val screenRatio = videoView.width / videoView.height.toFloat()
    val scaleX = videoRatio / screenRatio
    if (scaleX >= 1f) {
        videoView.scaleX = scaleX
    } else {
        videoView.scaleY = 1f / scaleX
    }
}

这对我有用。希望这可以帮助某人。


1
这是对我有效的解决方案。我只需要删除属性“android:visibility =”gone“即可。 - Cerise
这也是我唯一有效的解决方案,我使用了 Kotlin。 - Pedro Antonio
1
不工作,我不知道为什么。 - zihadrizkyef
非常惊人的解决方案,非常感谢 :) - Sherif farid
2023年对我有效! - Veer
显示剩余2条评论

11
解决方法是使用TextureView代替VideoView(SurfaceView)。
TextureView不会对内容进行任何操作以适应屏幕。
以下是解决方案的代码示例:
//store the SurfaceTexture to set surface for MediaPlayer
mTextureView.setSurfaceTextureListener(new SurfaceTextureListener() {
@Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface,
            int width, int height) {
        FullScreenActivity.this.mSurface = surface;

    }

....

Surface s = new Surface(mSurface);
mPlayer = mp;
mp.setSurface(s);

scaleVideo(mp);//<-- this function scales video to run cropped

....

private void scaleVideo(MediaPlayer mPlayer) {

        LayoutParams videoParams = (LayoutParams) mTextureView
                .getLayoutParams();
        DisplayMetrics dm = new DisplayMetrics();
        FullScreenActivity.this.getWindowManager().getDefaultDisplay()
                .getMetrics(dm);

        final int height = dm.heightPixels;
        final int width = dm.widthPixels;
        int videoHeight = mPlayer.getVideoHeight();
        int videoWidth = mPlayer.getVideoWidth();
        double hRatio = 1;

        hRatio = (height * 1.0 / videoHeight) / (width * 1.0 / videoWidth);
        videoParams.x = (int) (hRatio <= 1 ? 0 : Math.round((-(hRatio - 1) / 2)
                * width));
        videoParams.y = (int) (hRatio >= 1 ? 0 : Math
                .round((((-1 / hRatio) + 1) / 2) * height));
        videoParams.width = width - videoParams.x - videoParams.x;
        videoParams.height = height - videoParams.y - videoParams.y;
        Log.e(TAG, "x:" + videoParams.x + " y:" + videoParams.y);
        mTextureView.setScaleX(1.00001f);//<-- this line enables smoothing of the picture in TextureView.
        mTextureView.requestLayout();
        mTextureView.invalidate();

    }

4
这是一个使用TextureView并添加了centerCrop的库:https://github.com/dmytrodanylyk/android-video-crop - Jordy

3
我只是将视频放置在ConstraintLayout中,并使用这些参数。这有助于拉伸视频并实现android:scaleType="centerCrop"效果。
<VideoView
    android:id="@+id/video_view"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_gravity="center_horizontal"
    android:layout_width="0dp"
    android:layout_height="0dp" />

2

要在全屏模式下裁剪中心位置,您仍然可以使用VideoView。在RelativeLayout内将VideoView的宽度和高度设置为与父级匹配,并将其调整为大于屏幕大小并设置其位置。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout"
tools:context="com.example.Activity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_height="match_parent">

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

    </RelativeLayout>
</RelativeLayout>

然后在 onCreate 中:

   RelativeLayout rootView=(RelativeLayout) findViewById(R.id.rootLayout);
    Display display=getWindowManager().getDefaultDisplay();
    Point size=new Point();
    display.getSize(size);

    FrameLayout.LayoutParams rootViewParams = (FrameLayout.LayoutParams) rootView.getLayoutParams();
    int videoWidth=864;
    int videoHeight=1280;

    if ((float)videoWidth/(float)videoHeight<(float)size.x/(float)size.y) {
        rootViewParams.width=size.x;
        rootViewParams.height=videoHeight*size.x/videoWidth;
        rootView.setX(0);
        rootView.setY((rootViewParams.height-size.y)/2*-1);
    } else {
        rootViewParams.width=videoWidth*size.y/videoHeight;
        rootViewParams.height=size.y;
        rootView.setX((rootViewParams.width-size.x)/2*-1);
        rootView.setY(0);
    }
    rootView.setLayoutParams(rootViewParams);


    final VideoView mVideoView=(VideoView)findViewById(R.id.video_view);
    mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.splash));
    mVideoView.requestFocus();

    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            mVideoView.start();
        }
    });

我曾尝试过类似的解决方案,因为4年前当被问及此问题时,这个解决方案对我来说是显而易见的。那时VideoView强制将视频适应其内部,这禁止了裁剪。不知道现在它是如何工作的,因为4年过去了,SDK中一定有所改变。 - Vladyslav Matviienko
好的,我想象过了,但我花了一些时间寻找解决方案,而且这个问题很明确,在谷歌的前几个位置显示出来,所以我决定回答。在 API 级别 25 中,这对我有效。 - MazarD
你确定它应该是“裁剪”,而不是“居中适应”吗? - Vladyslav Matviienko
你是完全正确的,它确实有拉伸问题。我更换了视频,但它仍然出现问题。我刚刚编辑了答案,使用RelativeLayout中的VideoView来实现正确的解决方案,并调整大小和位置以使其超出屏幕。这最终起作用了。真不敢相信如此简单的任务竟然如此困难。 - MazarD
这是唯一有效的解决方案。非常感谢你。 - Bucky
显示剩余3条评论

0
我找到了一个解决方案:默认行为就像是 fitCenter,所以我计算了视频的宽高比和屏幕的宽高比,然后将 VideoView 缩放到全屏。结果就像是 centerCrop

0
补充一下@Nabin的回答。如果你在暂停时遇到问题,如果视频暂停导致视频视图高度发生变化并在下方显示黑屏,可以使用这个方法。
public class CustomVideoView extends VideoView {
private int originalWidth = 0;
private int originalHeight = 0;

public CustomVideoView(Context context) {
    super(context);
}

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

public CustomVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (originalWidth == 0 || originalHeight == 0) {
        originalWidth = MeasureSpec.getSize(widthMeasureSpec);
        originalHeight = MeasureSpec.getSize(heightMeasureSpec);
    }

    setMeasuredDimension(originalWidth, originalHeight);
}

}


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