From cd53e389a086299d3626ed9d9334c0ff5325620d Mon Sep 17 00:00:00 2001 From: Arjan Scherpenisse Date: Wed, 11 Jan 2017 13:47:56 +0100 Subject: [PATCH] Android: Resize the video correctly on layout changes (#432) The transform matrix was not being recalculated when the View dimensions changed. Fixed by copying the code from the scaleVideoSize method from the ScalableVideoView. https://github.com/yqritc/Android-ScalableVideoView/blob/master/library/src/main/java/com/yqritc/scalablevideoview/ScalableVideoView.java#L95 --- .../com/brentvatne/react/ReactVideoView.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/android/src/main/java/com/brentvatne/react/ReactVideoView.java b/android/src/main/java/com/brentvatne/react/ReactVideoView.java index 12dbef58..3db80d39 100644 --- a/android/src/main/java/com/brentvatne/react/ReactVideoView.java +++ b/android/src/main/java/com/brentvatne/react/ReactVideoView.java @@ -1,6 +1,7 @@ package com.brentvatne.react; import android.content.res.AssetFileDescriptor; +import android.graphics.Matrix; import android.media.MediaPlayer; import android.net.Uri; import android.os.Handler; @@ -19,6 +20,8 @@ import com.yqritc.scalablevideoview.ScalableVideoView; import com.android.vending.expansion.zipfile.APKExpansionSupport; import com.android.vending.expansion.zipfile.ZipResourceFile; +import com.yqritc.scalablevideoview.ScaleManager; +import com.yqritc.scalablevideoview.Size; import java.io.IOException; import java.util.HashMap; @@ -140,6 +143,30 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP return super.onTouchEvent(event); } + @Override + protected void onLayout(boolean changed, int left, int top, int right, int bottom) { + super.onLayout(changed, left, top, right, bottom); + + if (!changed || !mMediaPlayerValid) { + return; + } + + int videoWidth = getVideoWidth(); + int videoHeight = getVideoHeight(); + + if (videoWidth == 0 || videoHeight == 0) { + return; + } + + Size viewSize = new Size(getWidth(), getHeight()); + Size videoSize = new Size(videoWidth, videoHeight); + ScaleManager scaleManager = new ScaleManager(viewSize, videoSize); + Matrix matrix = scaleManager.getScaleMatrix(mScalableType); + if (matrix != null) { + setTransform(matrix); + } + } + private void initializeMediaPlayerIfNeeded() { if (mMediaPlayer == null) { mMediaPlayerValid = false;