【问题标题】:Android VideoView Proportional ScalingAndroid VideoView 比例缩放
【发布时间】:2012-07-28 23:53:14
【问题描述】:

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

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

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

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

【问题讨论】:

标签: android android-layout video android-videoview


【解决方案1】:

虽然为时已晚,但它可能会帮助其他人寻找同样的问题。以下答案保持纵横比(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;
}

【讨论】:

  • 真的很好用。无需寻找其他代码。谢谢 negi
  • 此解决方案存在一个问题 - 当在浏览器中使用时,调整大小的视图会“溢出”到下一页,因为在纵向视频中,横向视频会将视频视图设置为非常宽。调整大小后有没有办法居中裁剪?
  • 您可以将 VideoView 包装在具有页面尺寸的 View 中。
【解决方案2】:

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

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

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

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

【讨论】:

    【解决方案3】:

    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>
    

    查看more details的官方文档

    【讨论】:

      猜你喜欢
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      相关资源
      最近更新 更多