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?
7 Answers
(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);
}
}

- 955
- 1
- 7
- 13
-
Beautiful clean solution, and the only one here that actually works. – Oliver Hausler Oct 10 '14 at 14:32
-
1Dint work for me, I have 720 * 480 video and i'm trying to show it on a square view, the video is being "squashed" length wise – Roee May 23 '17 at 05:31
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);
}
}

- 5,594
- 2
- 25
- 22
-
I had a problem with this , trying to play a video, the VideoView was scaling first the width obtaining a leftAdjustment and then the height so the original leftAdjustment was set to 0 I solved it removing the initialization of the variables to the class private int leftAdjustment = 0; private int topAdjustment = 0; and removing leftAdjustment = 0; topAdjustment = 0; from inside the method Thanks for the solution, a good way to crop the video without having to use TextureView API 14 and loosing compatibilty – AntPachon Feb 20 '15 at 18:42
-
you should change layout method line to `super.layout(l + leftAdjustment, t + topAdjustment, r - leftAdjustment, b - topAdjustment);` . Otherwise it will just move your vidoView, but not change it's size – rstk Nov 30 '15 at 19:03
-
I suppose that scaling is reached by setMeasuredDimension() in onMeasure(). I used that code in https://play.google.com/store/apps/details?id=ru.zenrus.android , and it worked. Have you checked your suggestion? – FeelGood Dec 01 '15 at 12:45
-
This doesn't work for me. My `VideoView` takes just a half of screen and I need scale-crop a video, but this solution just maximize the view, not crop a video – Konstantin Konopko Apr 13 '22 at 21:02
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!

- 30,049
- 21
- 112
- 147
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.
Can I force Android to render a video into a smaller or larger rect?
Yes: make your VideoView
be the size you want, and Android will scale to fit the size of the VideoView
.

- 986,068
- 189
- 2,389
- 2,491
-
1
-
2I should add that not all platforms actually do this correctly. For Example, the Droid Eris seems to scale the video up to it's native resolution, expanding the video view as it goes. Platform fragmentation ahoy!! – haseman Jan 21 '10 at 18:57
-
I've found, further, that I can break the aspect ratio on the video view. I just have to wait for the video to start, once the Video View re-sizes itself I can then go in and muck with the view size using a layout. At that point, I can give it any size and it will stretch to the desired height/width. – haseman Mar 03 '10 at 21:06
-
1@haseman: I found that also the HTC Desire with 2.1 doesn't scale the video (at least with the current code I'm using) which works well though on all other devices like Nexus One, Samsung Galaxy S, etc. - both also running on 2.1. – Mathias Conradt Jul 07 '10 at 00:46
To FeelGoods answer.
Without the layout() method the "centerCrop" scale type is better, but the VideoView must be in a FrameLayout.
@Override
public void layout(int l, int t, int r, int b) {
super.layout(l + leftAdjustment, t + topAdjustment, r + leftAdjustment, b + topAdjustment);
}

- 395
- 4
- 9
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.
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();

- 828
- 9
- 16
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.
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.
Hope it helps.

- 500
- 5
- 22