我在找像ImageView.ScaleType中的CENTER_CROP这样的东西
均匀缩放图像(保持图像的纵横比),使图像的两个维度(宽度和高度)都等于或大于视图的相应尺寸(减去填充)。然后,图像在视图中居中。在XML中,使用以下语法: android:scaleType="centerCrop“
但对于VideoView来说。真的存在这样的东西吗?
发布于 2014-04-11 19:41:21
只有使用TextureView才能做到这一点。(surfaceView也不能工作)。这是一个库,用于在具有中心裁剪功能的textureView中播放视频。不幸的是,TextureView只能在API14或更高级别中使用。
https://github.com/dmytrodanylyk/android-video-crop
另一种可能是恰到好处地放大videoview,但我还没有尝试过。
发布于 2019-11-27 19:34: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>
然后
在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
}
}
在这里查看我的Java版本的答案:https://stackoverflow.com/a/59069357/6255841
这对我很有效。
发布于 2020-01-16 05:26:36
纳宾的回答对我很有效。
以下是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);
}
}
});
https://stackoverflow.com/questions/19601921
复制相似问题