Android 是否支持缩放视频?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2068242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does Android support scaling Video?
提问by haseman
Using a VideoView is it possible to set a scale factor for Android? By Default the video view resizes itself to fit the encoded resolution of the Video. Can I force Android to render a video into a smaller or larger rect?
使用 VideoView 是否可以为 Android 设置比例因子?默认情况下,视频视图会调整自身大小以适应视频的编码分辨率。我可以强制 Android 将视频渲染成更小或更大的矩形吗?
采纳答案by CommonsWare
By Default the video view resizes itself to fit the encoded resolution of the Video.
默认情况下,视频视图会调整自身大小以适应视频的编码分辨率。
VideoView
(or the SurfaceView
for use with MediaPlayer
) will be the size you tell it to be in your layout. Within that space, the video will play back as large as possible while maintaining the aspect ratio.
VideoView
(或SurfaceView
用于MediaPlayer
)将是您告诉它在您的布局中的大小。在该空间内,视频将在保持纵横比的同时尽可能大地播放。
Can I force Android to render a video into a smaller or larger rect?
我可以强制 Android 将视频渲染成更小或更大的矩形吗?
Yes: make your VideoView
be the size you want, and Android will scale to fit the size of the VideoView
.
是的:使您VideoView
成为您想要的大小,Android 将缩放以适合VideoView
.
回答by Micha? Kowalczuk
(I know it's very old question, but there is another way to control dimensions, which isn't described here, maybe someone will find it helpful.)
(我知道这是一个很老的问题,但是还有另一种控制维度的方法,这里没有描述,也许有人会发现它有帮助。)
Declare your own MyVideoViewclass in your layout and write your own onMeasure()method. Here is how to run video stretched to original View's dimensions:
在您的布局中声明您自己的MyVideoView类并编写您自己的onMeasure()方法。以下是如何运行拉伸到原始视图尺寸的视频:
public class MyVideoView extends VideoView {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = getDefaultSize(0, widthMeasureSpec);
int height = getDefaultSize(0, heightMeasureSpec);
setMeasuredDimension(width, height);
}
}
回答by FeelGood
To set "centerCrop"
scale type for VideoView
your onMeasure()
and layout()
methods may look like this:
"centerCrop"
为VideoView
您的onMeasure()
和layout()
方法设置比例类型可能如下所示:
public class CenterCropVideoView extends VideoView {
private int leftAdjustment;
private int topAdjustment;
...
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int videoWidth = getMeasuredWidth();
int videoHeight = getMeasuredHeight();
int viewWidth = getDefaultSize(0, widthMeasureSpec);
int viewHeight = getDefaultSize(0, heightMeasureSpec);
leftAdjustment = 0;
topAdjustment = 0;
if (videoWidth == viewWidth) {
int newWidth = (int) ((float) videoWidth / videoHeight * viewHeight);
setMeasuredDimension(newWidth, viewHeight);
leftAdjustment = -(newWidth - viewWidth) / 2;
} else {
int newHeight = (int) ((float) videoHeight / videoWidth * viewWidth);
setMeasuredDimension(viewWidth, newHeight);
topAdjustment = -(newHeight - viewHeight) / 2;
}
}
@Override
public void layout(int l, int t, int r, int b) {
super.layout(l + leftAdjustment, t + topAdjustment, r + leftAdjustment, b + topAdjustment);
}
}
回答by Adil Hussain
I find that when a VideoView is placed inside a RelativeLayout, the video stretches bothheight and width to fit the VideoView's specified height and width (irrespective of the video aspect ratio). However, when I place the VideoView in a FrameLayout, the video stretches height and width until it matches one ofthe VideoView's specified height or width (i.e. it does not break aspect ratio). Strange, I know, but that's what I found!
我发现,当一个VideoView被放置在里面的RelativeLayout,视频伸展两者的高度和宽度,以适应VideoView的指定的高度和宽度(不考虑视频的纵横比的)。但是,当我将 VideoView 放在 FrameLayout 中时,视频会拉伸高度和宽度,直到它与VideoView 指定的高度或宽度之一匹配(即它不会破坏纵横比)。奇怪,我知道,但这就是我发现的!
回答by silver_mx
I am also trying to achieve that and so far this has worked ok. The idea is to use the setLayoutParams for the video view and specify the size. It is just a simple fragment but it gives the idea. Check the LayoutParams lines.
我也在努力实现这一目标,到目前为止一切顺利。这个想法是对视频视图使用 setLayoutParams 并指定大小。它只是一个简单的片段,但它给出了想法。检查 LayoutParams 行。
VideoView mVideoView = new VideoView(this);
//intermediate code
mVideoView.setVideoPath("/sdcard/VIDEO0007.3gp");
MediaController mController = new MediaController(this);
mVideoView.setMediaController(mController);
mController.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int width = mVideoView.getMeasuredWidth();
int height = mVideoView.getMeasuredHeight();
//we add 10 pixels to the current size of the video view every time you touch
//the media controller.
LayoutParams params = new LinearLayout.LayoutParams(width+10, height+10);
mVideoView.setLayoutParams(params);
return true;
}
});
mVideoView.requestFocus();
mVideoView.start();
回答by Joao Ferreira
To FeelGoodsanswer.
给FeelGoods回答。
Without the layout() method the "centerCrop"scale type is better, but the VideoView must be in a FrameLayout.
如果没有 layout() 方法,“ centerCrop”比例类型更好,但 VideoView 必须在 FrameLayout 中。
@Override
public void layout(int l, int t, int r, int b) {
super.layout(l + leftAdjustment, t + topAdjustment, r + leftAdjustment, b + topAdjustment);
}
回答by Murali
Guys I had other idea create your own VideoView class by extending VideoView, with that u can do what ever u want,. exactly u have to create couple of methods in your VideoView.
伙计们,我有其他想法通过扩展 VideoView 创建您自己的 VideoView 类,这样您就可以做任何您想做的事情。确切地说,您必须在 VideoView 中创建几个方法。
public void setDimensions(int w, int h) {
this.mForceHeight = h;
this.mForceWidth = w;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(mForceWidth, mForceHeight);
}
make your own constructor for ur VideoView class and make sure this methods in ur own videoview class then use it as like androids default videoview into both xml and class files.
为您的 VideoView 类制作您自己的构造函数,并确保在您自己的 videoview 类中使用此方法,然后将其用作 xml 和 class 文件中的 androids 默认视频视图。
Hope it helps.
希望能帮助到你。