【问题标题】:Center Crop an Android VideoView居中裁剪 Android VideoView
【发布时间】:2013-11-05 06:50:05
【问题描述】:

我正在寻找类似 @​​987654321@ 中的 CENTER_CROP 的东西

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

但对于 VideoView。有这样的东西吗?

【问题讨论】:

标签: android scale center crop android-videoview


【解决方案1】:

如果您使用的是 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

这对我有用。

【讨论】:

  • 这是迄今为止我见过的最好的解决方案。放入一个包装器和一个 lambda,就完美了!
  • 不幸的是,它对我不起作用(为什么?它不能扩展,保持不变
  • @Nabin 有效!但是为什么不使用 FrameLayout 而不是 ConstraintLayout 呢?
【解决方案2】:

Nabin 的回答对我有用。

这里是 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);
        }
    }
});

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:
      //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;
      
          }
      

      【讨论】:

        猜你喜欢
        • 2011-05-23
        • 2013-07-18
        • 2023-03-05
        • 2011-10-05
        • 2013-05-14
        • 2015-02-02
        • 2019-10-23
        • 2021-07-05
        • 1970-01-01
        相关资源
        最近更新 更多