Stretch video to full screen in a SurfaceView extension
I have created a widget that is an extension of SurfaceView
(very similar to VideoView) and I am working on a feature to stretch the video fully across the device screen when certain action is taken. I've looked at onMeasure
function of VideoView
and re-wrote it this way:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mStretchVideo) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} else {
int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
if (mVideoWidth > 0 && mVideoHeight > 0) {
if (mVideoWidth * height > width * mVideoHeight) {
height = width * mVideoHeight / mVideoWidth;
} else if (mVideoWidth * height < width * mVideoHeight) {
width = height * mVideoWidth / mVideoHeight;
}
}
setMeasuredDimension(width, height);
}
}
This seems to work fine if I completely stop the video and start playing again. Now I am trying to force the refresh of this SurfaceView
after setting the stretch flag so t开发者_开发知识库hat the video gets stretched while it is playing, but I couldn't figure out how to force a refresh on SurfaceView
. I tried android.view.View.invalidate()
, android.view.View.refreshDrawableState()
and calling android.view.View.measure(int, int)
directly with different combinations but did not get any success. Any ideas?
No need of code to play video in full screen mode
Apply the following layout format over the xml containing the videoview it will for sure will play the video in full screen mode. as it is running mine :) Hope it helps
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<VideoView android:id="@+id/myvideoview"
android:layout_width="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="fill_parent">
</VideoView>
</RelativeLayout>
You can call the measure
method of the SurfaceView
from the Activity like this:
Display display = getWindowManager().getDefaultDisplay();
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(display.getWidth(),
MeasureSpec.UNSPECIFIED);
int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(display.getHeight(),
MeasureSpec.UNSPECIFIED);
surfaceView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Javanator is the correct answer. There is no need for additional code. Make sure your video view looks like this
<VideoView android:id="@+id/myvideoview"
android:layout_width="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="fill_parent">
精彩评论