Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Android support scaling Video?

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?

like image 699
haseman Avatar asked Jan 14 '10 23:01

haseman


3 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);
 }
}
like image 164
Michał Kowalczuk Avatar answered Oct 16 '22 04:10

Michał Kowalczuk


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);
    }
}
like image 12
FeelGood Avatar answered Oct 16 '22 04:10

FeelGood


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!

like image 11
Adil Hussain Avatar answered Oct 16 '22 02:10

Adil Hussain