From 1a18a878a9a9db8bce024bd0ca2148fde68bee54 Mon Sep 17 00:00:00 2001 From: Brian Wernick Date: Sat, 2 Dec 2017 12:40:57 -0700 Subject: [PATCH] Added a FIT_XY option to the scale type (#538) --- .../main/res/layout/video_player_activity.xml | 1 + .../core/video/scale/MatrixManager.java | 30 ++++++++++++++----- .../exomedia/core/video/scale/ScaleType.java | 1 + library/src/main/res/values/attributes.xml | 3 +- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/demo/src/main/res/layout/video_player_activity.xml b/demo/src/main/res/layout/video_player_activity.xml index 4ded1050..93301124 100644 --- a/demo/src/main/res/layout/video_player_activity.xml +++ b/demo/src/main/res/layout/video_player_activity.xml @@ -8,5 +8,6 @@ android:id="@+id/video_play_activity_video_view" android:layout_width="match_parent" android:layout_height="match_parent" + app:videoScale="fitXY" app:useDefaultControls="true"/> diff --git a/library/src/main/java/com/devbrackets/android/exomedia/core/video/scale/MatrixManager.java b/library/src/main/java/com/devbrackets/android/exomedia/core/video/scale/MatrixManager.java index bca0b8d3..374e7de7 100644 --- a/library/src/main/java/com/devbrackets/android/exomedia/core/video/scale/MatrixManager.java +++ b/library/src/main/java/com/devbrackets/android/exomedia/core/video/scale/MatrixManager.java @@ -114,6 +114,9 @@ public boolean scale(@NonNull View view, @NonNull ScaleType scaleType) { case FIT_CENTER: applyFitCenter(view); break; + case FIT_XY: + applyFitXy(view); + break; case NONE: setScale(view, 1, 1); break; @@ -143,8 +146,8 @@ protected void applyCenter(@NonNull View view) { * @param view The view to apply the transformation to */ protected void applyCenterCrop(@NonNull View view) { - float xScale = (float)view.getWidth() / intrinsicVideoSize.x; - float yScale = (float)view.getHeight() / intrinsicVideoSize.y; + float xScale = (float) view.getWidth() / intrinsicVideoSize.x; + float yScale = (float) view.getHeight() / intrinsicVideoSize.y; float scale = Math.max(xScale, yScale); xScale = scale / xScale; @@ -161,7 +164,7 @@ protected void applyCenterCrop(@NonNull View view) { * @param view The view to apply the transformation to */ protected void applyCenterInside(@NonNull View view) { - if(intrinsicVideoSize.x <= view.getWidth() && intrinsicVideoSize.y <= view.getHeight()) { + if (intrinsicVideoSize.x <= view.getWidth() && intrinsicVideoSize.y <= view.getHeight()) { applyCenter(view); } else { applyFitCenter(view); @@ -175,8 +178,8 @@ protected void applyCenterInside(@NonNull View view) { * @param view The view to apply the transformation to */ protected void applyFitCenter(@NonNull View view) { - float xScale = (float)view.getWidth() / intrinsicVideoSize.x; - float yScale = (float)view.getHeight() / intrinsicVideoSize.y; + float xScale = (float) view.getWidth() / intrinsicVideoSize.x; + float yScale = (float) view.getHeight() / intrinsicVideoSize.y; float scale = Math.min(xScale, yScale); xScale = scale / xScale; @@ -184,6 +187,17 @@ protected void applyFitCenter(@NonNull View view) { setScale(view, xScale, yScale); } + /** + * Applies the {@link ScaleType#FIT_XY} to the specified matrix. This will + * scale the video so that both the width and height will always match that of + * the view + * + * @param view The view to apply the transformation to + */ + protected void applyFitXy(@NonNull View view) { + setScale(view, 1, 1); + } + /** * Applies the specified scale modification to the view * @@ -194,10 +208,10 @@ protected void applyFitCenter(@NonNull View view) { protected void setScale(@NonNull View view, float xScale, float yScale) { //If the width and height have been swapped, we need to re-calculate the scales based on the swapped sizes boolean currentWidthHeightSwapped = ((currentRotation / QUARTER_ROTATION) % 2) == 1; - if (currentWidthHeightSwapped){ + if (currentWidthHeightSwapped) { float scaleTemp = xScale; - xScale = yScale * view.getHeight() / view.getWidth(); - yScale = scaleTemp * view.getWidth() / view.getHeight(); + xScale = yScale * view.getHeight() / view.getWidth(); + yScale = scaleTemp * view.getWidth() / view.getHeight(); } view.setScaleX(xScale); diff --git a/library/src/main/java/com/devbrackets/android/exomedia/core/video/scale/ScaleType.java b/library/src/main/java/com/devbrackets/android/exomedia/core/video/scale/ScaleType.java index c4552d34..d663c82f 100644 --- a/library/src/main/java/com/devbrackets/android/exomedia/core/video/scale/ScaleType.java +++ b/library/src/main/java/com/devbrackets/android/exomedia/core/video/scale/ScaleType.java @@ -11,6 +11,7 @@ public enum ScaleType { CENTER_CROP, CENTER_INSIDE, FIT_CENTER, + FIT_XY, NONE; /** diff --git a/library/src/main/res/values/attributes.xml b/library/src/main/res/values/attributes.xml index 11b4af3b..b12baff6 100644 --- a/library/src/main/res/values/attributes.xml +++ b/library/src/main/res/values/attributes.xml @@ -11,7 +11,8 @@ - + + \ No newline at end of file