我想设置一个充满屏幕并保留其长宽比的视频。但是,我不希望视频与手机的长宽比匹配。
如果屏幕的长宽比为18.9:9,视频的长宽比为16:9,则当我将视频放在继承了fill_parent的VideoView中时,视频会被拉伸。如果未设置fill_parent,则视频将保留其长宽比,但仅占屏幕的1/3,因为长宽比是由其宽度而不是其高度决定的。这是不可取的。
我想用视频垂直填充屏幕,无论部分宽度是否被剪掉。
当前代码:
window.setFormat(PixelFormat.TRANSLUCENT)
val videoView = findViewById < VideoView > (videoView)
val video = Uri.parse("android.resource://" + packageName + "/" +
R.raw.bgvideo)
videoView.setVideoURI(video)
videoView.setOnPreparedListener {
mp: MediaPlayer - >
mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING)
mp.isLooping = true
mp.setScreenOnWhilePlaying(false)
}
videoView.start()
XML:
<VideoView
android:id="@+id/videoView"
android:layout_width="2000dp"
android:layout_height="match_parent" />
<!--- Sample Foreground Content !--->
<ImageView
android:layout_width="wrap_content"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="24dp"
android:contentDescription="@string/logo"
android:src="@drawable/logo" />
当前结果:
答案 0 :(得分:1)
以编程方式调整VideoView
的尺寸。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--parent needs to be matching the screen height-->
<!--VideoView layout_width doesn't matter-->
<VideoView
android:id="@+id/video_player"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
// at onCreate or anywhere
// post, because view dimension is not initialized yet in here
videoView.post(new Runnable() {
@Override
public void run() {
// do resizing here
// AR = aspect ratio
float videoARWidth = 16.f;
float videoARHeight = 9.f;
// Phone screen aspect ratio height
float screenARHeight = 9.f;
// scale to screen AR height
float videoScale = screenARHeight / videoARHeight;
float videoARRatio = videoARWidth / videoARHeight;
// scale the ratio to screen
float videoScaledARRatio = videoARRatio * videoScale;
ViewGroup.LayoutParams layoutParams = videoView.getLayoutParams();
// make sure the VideoView matches the screen height
layoutParams.width = (int)(videoView.getHeight() * videoScaledARRatio);
videoView.setLayoutParams(layoutParams);
}
});
此代码与屏幕宽度无关。
宽度将始终缩放到屏幕高度。实际上,它将始终缩放到其(VideoView)自己的高度。但是,正如我在代码上评论的那样,请使用layout_height =“ match_parent”来匹配屏幕。
如果屏幕宽度更大,我不确定是否希望宽度合适。
如果屏幕宽度更大,视频宽度将不会填满屏幕。