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?
(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 MyVideoView class in your layout and write your own onMeasure() method. Here is how to run video stretched to original View's dimensions:
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);
}
}
To set "centerCrop"
scale type for VideoView
your onMeasure()
and layout()
methods may look like this:
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);
}
}
I find that when a VideoView is placed inside a RelativeLayout, the video stretches both height 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 of the VideoView's specified height or width (i.e. it does not break aspect ratio). Strange, I know, but that's what I found!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With