I have some problems resizing SurfaceView in Android 2.3. version. I am working with a video player. I need to resize SurfaceView when the size of the video does not match the size of the surface. I have a fragment with a SurfaceView inside it. The fragment does not correspond to the full screen, but only part of it. For Android 4 versions, everything is fine, but in Android 2.3. after resizing, SurfaceView is larger than its parent FrameLayout. I have no idea why this is happening.
Here is an interface that allows init video size
void setSurfaceSize(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den);
@Override
public void setSurfaceSize(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den) {
if (width * height == 0)
return;
mVideoHeight = height;
mVideoWidth = width;
mVideoVisibleHeight = visible_height;
mVideoVisibleWidth = visible_width;
mSarNum = sar_num;
mSarDen = sar_den;
Message msg = mHandler.obtainMessage(SURFACE_SIZE);
mHandler.sendMessage(msg);
}
And second, resize the SurfaceView.
private void changeSurfaceSize() {
int sw;
int sh;
if (mPresentation == null) {
Log.d("MyLog", "Presentation is null");
sw = ((Activity)mainActivity).getWindow().getDecorView().getWidth();
sh = ((Activity)mainActivity).getWindow().getDecorView().getHeight();
} else {
Log.d("MyLog", "Presentation is not null");
sw = mPresentation.getWindow().getDecorView().getWidth();
sh = mPresentation.getWindow().getDecorView().getHeight();
}
double dw = sw, dh = sh;
boolean isPortrait;
if (mPresentation == null) {
isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
} else {
isPortrait = false;
}
if (sw > sh && isPortrait || sw < sh && !isPortrait) {
dw = sh;
dh = sw;
}
if (dw * dh == 0 || mVideoWidth * mVideoHeight == 0) {
Log.e(TAG, "Invalid surface size");
return;
}
double ar, vw;
double density = (double)mSarNum / (double)mSarDen;
if (density == 1.0) {
vw = mVideoVisibleWidth;
ar = (double)mVideoVisibleWidth / (double)mVideoVisibleHeight;
} else {
vw = mVideoVisibleWidth * density;
ar = vw / mVideoVisibleHeight;
}
double dar = dw / dh;
switch (mCurrentSize) {
case SURFACE_BEST_FIT:
if (dar < ar)
dh = dw / ar;
else
dw = dh * ar;
break;
case SURFACE_FIT_HORIZONTAL:
dh = dw / ar;
break;
case SURFACE_FIT_VERTICAL:
dw = dh * ar;
break;
case SURFACE_FILL:
break;
case SURFACE_16_9:
ar = 16.0 / 9.0;
if (dar < ar)
dh = dw / ar;
else
dw = dh * ar;
break;
case SURFACE_4_3:
ar = 4.0 / 3.0;
if (dar < ar)
dh = dw / ar;
else
dw = dh * ar;
break;
case SURFACE_ORIGINAL:
dh = mVideoVisibleHeight;
dw = vw;
break;
}
SurfaceView surface;
SurfaceView subtitlesSurface;
SurfaceHolder surfaceHolder;
SurfaceHolder subtitlesSurfaceHolder;
FrameLayout surfaceFrame;
if (mPresentation == null) {
surface = mSurface;
subtitlesSurface = mSubtitlesSurface;
surfaceHolder = mSurfaceHolder;
subtitlesSurfaceHolder = mSubtitlesSurfaceHolder;
surfaceFrame = mSurfaceFrame;
} else {
surface = mPresentation.mSurface;
subtitlesSurface = mPresentation.mSubtitlesSurface;
surfaceHolder = mPresentation.mSurfaceHolder;
subtitlesSurfaceHolder = mPresentation.mSubtitlesSurfaceHolder;
surfaceFrame = mPresentation.mSurfaceFrame;
}
surfaceHolder.setFixedSize(mVideoWidth, mVideoHeight);
subtitlesSurfaceHolder.setFixedSize(mVideoWidth, mVideoHeight);
LayoutParams lp = surface.getLayoutParams();
lp.width = (int) Math.ceil(dw * mVideoWidth / mVideoVisibleWidth);
lp.height = (int) Math.ceil(dh * mVideoHeight / mVideoVisibleHeight);
surface.setLayoutParams(lp);
subtitlesSurface.setLayoutParams(lp);
lp = surfaceFrame.getLayoutParams();
lp.width = (int) Math.floor(dw);
lp.height = (int) Math.floor(dh);
surfaceFrame.setLayoutParams(lp);
surface.invalidate();
subtitlesSurface.invalidate();
}
Does anyone have any idea why this might happen? I will be very happy for any help !!! Thank!