Stretch video in full screen in SurfaceView extension

I created a widget that is an extension of SurfaceView (very similar to VideoView ), and I'm working on stretching the video all the way to the device’s screen when performing certain actions. I looked at the onMeasure VideoView function and rewrote it as follows:

 @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); } } 

It seems like it's okay if I stop the video completely and start playing again. Now I am trying to force update this SurfaceView after setting the stretch frame so that the video is stretched while it is playing, but I could not figure out how to force the update on the SurfaceView . I tried android.view.View.invalidate() , android.view.View.refreshDrawableState() and called android.view.View.measure(int, int) directly with various combinations, but did not succeed. Any ideas?

+4
source share
3 answers

No code needed to play video in full screen

Apply the following layout format to the xml containing the video ad, and it will certainly play the video in full screen. how it works mine :) Hope this 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> 
+2
source

You can call the measure method for SurfaceView from Activity as follows:

  Display display = getWindowManager().getDefaultDisplay(); int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(display.getWidth(), MeasureSpec.UNSPECIFIED); int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(display.getHeight(), MeasureSpec.UNSPECIFIED); surfaceView.measure(childWidthMeasureSpec, childHeightMeasureSpec); 
+1
source

Javanator is the correct answer. No need for additional code. Make sure your video image 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"> 
0
source

Source: https://habr.com/ru/post/1312584/


All Articles