Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize VideoView

I am attempting to dynamically set the size of an Android VideoView. I have looked on StackOverflow as well as the internet; and the best solution I found was from here. I have put my implementation below:

public class ResizableVideoView extends VideoView {

    public ResizableVideoView(Context c) {
        super(c);
    }

    private int mVideoWidth = 100;
    private int mVideoHeight = 100;

    public void changeVideoSize(int width, int height) {
        mVideoWidth = width;      
        mVideoHeight = height;

        // not sure whether it is useful or not but safe to do so
        getHolder().setFixedSize(width, height); 
        
        forceLayout();
        invalidate(); // very important, so that onMeasure will be triggered
    } 

    public void onMeasure(int specwidth, int specheight) {
        Log.i("onMeasure","On Measure has been called");
        setMeasuredDimension(mVideoWidth, mVideoHeight);
    }

    public void onDraw(Canvas c) {
        super.onDraw(c);
        Log.i("onDraw","Drawing...");
    }
}

The video resizes correctly on the Android emulator as well as on a Motorola Droid X; but on a Motorola Droid, the VideoView resizes but the video playing in the VideoView does not resize. On the Motorola Droid, if the VideoView is set to a larger dimension than the video playing, a black background appears in the VideoView with the video playing in the top left corner of the VideoView on top of the black background.

How does one resize a VideoView properly in Android?

Thanks, Vance

like image 515
Vance-Turner Avatar asked Jun 14 '12 19:06

Vance-Turner


1 Answers

My implementation works like this:

RelativeLayout.LayoutParams videoviewlp = new RelativeLayout.LayoutParams(newwidth, newheight);
    videoviewlp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    videoviewlp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
    videoview.setLayoutParams(videoviewlp);
    videoview.invalidate();

By invalidating the videoview, you will force it to redraw the whole videoview using the new LayoutParams (and the new dimensions).

like image 113
Ceetn Avatar answered Nov 07 '22 07:11

Ceetn