27

I am looking for something like the CENTER_CROP in ImageView.ScaleType

Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). The image is then centered in the view. From XML, use this syntax: android:scaleType="centerCrop"

but for a VideoView. Does anything like this exist?

4

4 回答 4

16

您只能使用 TextureView 来实现这一点。(surfaceView 也不起作用)。这是一个在带有中心裁剪功能的纹理视图中播放视频的库。不幸的是,TextureView 只能在 api 级别 14 及更高级别中使用。

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

另一种可能性是放大视频视图恰到好处,但我还没有尝试过。

于 2014-04-11T11:41:21.227 回答
10

如果您使用ConstraintLayout,这是一种简单易行的方法。

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>

然后

在科特林:

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
    }
}

在此处查看我的 Java 版本答案: https ://stackoverflow.com/a/59069357/6255841

这对我有用。

于 2019-11-27T11:34:09.527 回答
2

纳宾的回答对我有用。

这是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);
        }
    }
});
于 2020-01-15T21:26:35.983 回答
1
//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;

    }
于 2014-02-22T05:22:05.157 回答