0

In my android video player, how can I set the size of video, so that it can fit in the view.

I've created a CustomView extends SurfaceView. in the overriden onMeasure(), I'm thinking of calculating the size of my CustomView (which displays the video).

Then pass those dimensions in setMeasuredDimension(width, height);.

I've not come across any other method to set the dimensions of video manually.

Thanks

reiley
  • 3,759
  • 12
  • 58
  • 114

1 Answers1

0

Perfect example here, or look at this,

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int desiredWidth = 100;
int desiredHeight = 100;

int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);

int width;
int height;

//Measure Width
if (widthMode == MeasureSpec.EXACTLY) {
    //Must be this size
    width = widthSize;
} else if (widthMode == MeasureSpec.AT_MOST) {
    //Can't be bigger than...
    width = Math.min(desiredWidth, widthSize);
} else {
    //Be whatever you want
    width = desiredWidth;
}

//Measure Height
if (heightMode == MeasureSpec.EXACTLY) {
    //Must be this size
    height = heightSize;
} else if (heightMode == MeasureSpec.AT_MOST) {
    //Can't be bigger than...
    height = Math.min(desiredHeight, heightSize);
} else {
    //Be whatever you want
    height = desiredHeight;
}

//MUST CALL THIS
setMeasuredDimension(width, height);
}

Hope its help.

Community
  • 1
  • 1
Igor Morais
  • 645
  • 1
  • 8
  • 13