中心裁剪Android VideoView

clo*_*ith 20 android center crop scale android-videoview

我正在寻找像ImageView.ScaleType中的CENTER_CROP

均匀缩放图像(保持图像的纵横比),使图像的尺寸(宽度和高度)等于或大于视图的相应尺寸(减去填充).然后图像在视图中居中.从XML,使用以下语法:android:scaleType ="centerCrop"

但对于VideoView.有这样的事吗?

Jor*_*rdy 15

您只能使用TextureView实现此目的.(surfaceView也不会工作).这是一个用于在带有中心裁剪功能的te​​xtureView中播放视频的库.不幸的是,TextureView只能在api level 14及更高版本中使用.

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

另一种可能性就是放大视频视图,但我还没有尝试过.


Nab*_*bin 9

使用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>
Run Code Online (Sandbox Code Playgroud)

然后

在科特林:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

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

这对我有用。

  • 这是迄今为止我见过的最好的解决方案。添加一个包装纸和一个 lambda,就完美了! (2认同)