20

在Android的VideoView中,有没有办法实现和ImageView.ScaleType.CENTER_CROP一样的效果?

也就是说,我希望我的 VideoView 播放视频,使其充满整个屏幕而不会失真。如果视频纵横比不完全适合屏幕,则应裁剪而不是扭曲。

以下解决方案将填满屏幕,但不保持视频的宽高比: https ://stackoverflow.com/a/6927300/1068656

并且此解决方案保持了视频的纵横比,但不会填满整个屏幕(视频被缩放直到较长的一侧碰到屏幕的边缘,从而在侧面引入了条): https ://stackoverflow.com/a/4855315/1068656

4

3 回答 3

15

虽然为时已晚,但它可能会帮助其他人寻找同样的问题。以下答案保持纵横比(videoProportion)。视频视图的额外部分被电话视图裁剪。

private void setDimension() {
     // Adjust the size of the video
     // so it fits on the screen
     float videoProportion = getVideoProportion();
     int screenWidth = getResources().getDisplayMetrics().widthPixels;
     int screenHeight = getResources().getDisplayMetrics().heightPixels;
     float screenProportion = (float) screenHeight / (float) screenWidth;
     android.view.ViewGroup.LayoutParams lp = videoView.getLayoutParams();

     if (videoProportion < screenProportion) {
         lp.height= screenHeight;
         lp.width = (int) ((float) screenHeight / videoProportion);
     } else {
         lp.width = screenWidth;
         lp.height = (int) ((float) screenWidth * videoProportion);
     }
     videoView.setLayoutParams(lp);
 }

// This method gets the proportion of the video that you want to display.
// I already know this ratio since my video is hardcoded, you can get the  
// height and width of your video and appropriately generate  the proportion  
//    as :height/width 
private float getVideoProportion(){
  return 1.5f;
}
于 2014-10-27T12:13:40.473 回答
12

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

在科特林:

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 中:

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 / scale);
      }
   }
});

这对我有用。希望这会对某人有所帮助。

于 2020-04-21T03:19:16.073 回答
1

ConstraintLayout具有app:layout_constraintDimensionRatio用于此目的的属性。

<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:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="4:3" <!-- w:h -->
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</ConstraintLayout>

更多详情请查看官方文档

于 2020-09-15T17:27:13.977 回答