Skip to content

Commit

Permalink
Added a FIT_XY option to the scale type (brianwernick#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianwernick authored Dec 2, 2017
1 parent ef46d9f commit 1a18a87
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions demo/src/main/res/layout/video_player_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -175,15 +178,26 @@ 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;
yScale = scale / yScale;
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 <code>view</code>
*
* @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
*
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum ScaleType {
CENTER_CROP,
CENTER_INSIDE,
FIT_CENTER,
FIT_XY,
NONE;

/**
Expand Down
3 changes: 2 additions & 1 deletion library/src/main/res/values/attributes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<enum name="centerCrop" value="1"/>
<enum name="centerInside" value="2"/>
<enum name="fitCenter" value="3"/>
<enum name="none" value="4"/>
<enum name="fitXY" value="4"/>
<enum name="none" value="5"/>
</attr>
</declare-styleable>
</resources>

0 comments on commit 1a18a87

Please sign in to comment.