将视频放置在 VideoView 中
因此,我扩展了 VideoView 的 onMeasure 来放大视频以适合全屏视图。
方法如下:
public void setVideoAspect(int w,int h){
wVideo=w;
hVideo=h;
onMeasure(w, h);
}
@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(wVideo!=0 && hVideo!=0)
setMeasuredDimension(wVideo,hVideo);
}
我使用屏幕的显示指标(宽度、高度)调用 setVideoAspect() 。问题在于此方法会拉伸视频以适合屏幕内部。我希望能够保持纵横比。 (我有 4:3 视频和 3:2 屏幕尺寸。)我使用以下代码为视图提供保留比例测量值:
int height = (int) (metrics.widthPixels*3/(float)4);
int width= metrics.widthPixels;
mVideoView.setVideoAspect(width,height);
所以这可以完成工作,但有一个问题:它给了我一个 4:3 视频屏幕的宽度并正确缩放高度,但它不会使视频居中。 (它只是裁剪视频的底部部分,而不是同等地裁剪顶部和底部。)我有一个包含 VideoView 的相对布局,并将 VideoView 的重力设置为中心。
So I have extended VideoView's onMeasure to scale up the video to fit inside a fullscreen view.
here is how:
public void setVideoAspect(int w,int h){
wVideo=w;
hVideo=h;
onMeasure(w, h);
}
@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(wVideo!=0 && hVideo!=0)
setMeasuredDimension(wVideo,hVideo);
}
I call setVideoAspect() with the display metrics (width, hight) of the screen. The problem is that this method stretches the video to fit inside the screen. I want to be able to keep the aspect ratio. (I have 4:3 video and 3:2 screen size.) I used the folowing code to give the retained ratio measurements to the view:
int height = (int) (metrics.widthPixels*3/(float)4);
int width= metrics.widthPixels;
mVideoView.setVideoAspect(width,height);
So this does the job but there is an issue: it gives me a 4:3 video with the width of the screen and scales the height correctly, but it doesn't center the video. (It just crops the bottom part of the video instead of the top and the bottom equally.) I have a relative layout containing the VideoView with the gravity of the VideoView set to center.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试使用
FrameLayout
代替。我不知道为什么,但如果我在代码中使用Linear
或Relative
它不会居中,但FrameLayout
会居中。下面是使我的视频适合屏幕的 XML,保留比例并将其居中:Try using a
FrameLayout
instead. I'm not sure why, but if I use aLinear
orRelative
in my code it won't center, butFrameLayout
does. Here is the XML that fit my video to the screen, preserving the ratio and centering it:为了使视频在 RelativeLayout 中居中,我添加了
layout_gravity="center"
广告layout_centerInParent="true"
。它适用于我的 Android 4.3 手机。In order to center the video in the RelativeLayout I added both
layout_gravity="center"
adlayout_centerInParent="true"
. It works on my Android 4.3 phone.这适用于任何保持视频宽高比的视频。它将视频定位在 VideoView 内部,并像 ImageView 一样执行 Center Crop 或 Center Inside。
我正在使用 VideoView 来覆盖整个 ConstraintLayout。您可以使用任何其他布局,可能将 match_parent 作为宽度和高度。
在 onCreate 中:
希望它对某人有帮助!
This works for any video keeping the video's aspect ratio. It positions the video inside the VideoView and performs a Center Crop or a Center Inside just like an ImageView.
I am using a VideoView to cover the whole ConstraintLayout. You can use any other layout probably with match_parent as width and height.
In onCreate:
Hope it helps someone!
卡梅伦以编程方式回答(以防像我这样的人需要它)此代码位于我的代码中活动的 onCreate 内部(下面的“this”指的是该活动)
Cameron's Answer in a programmatic way(in case someone like me needs it) This code is inside onCreate of an activity in my code( 'this' below refers to the activity)
如果您正在寻找与 VideoView 中的
ImageView.ScaleType.CENTER_CROP
功能相同的效果,那么这是一个简单易用的解决方案,
请在此处查看我的 XML 和 Kotlin 版本答案:
https://stackoverflow.com/a/59069292/6255841
在 JAVA 中:
If you are looking for the same effect as
ImageView.ScaleType.CENTER_CROP
feature in VideoView thenHere is a simple and easy solution
See my XML and Kotlin version answer here:
https://stackoverflow.com/a/59069292/6255841
In JAVA: