Skip to content

Commit f3c802d

Browse files
Victor Lagunafacebook-github-bot
Victor Laguna
authored andcommitted
Add support to fitBottomStart scaleType
Summary: Introducing fitBottomStart scaleType which scales the child to fit inside the parent, aspect ratio is preserved and child is aligned to bottom-left. Reviewed By: kirwan Differential Revision: D5543561 fbshipit-source-id: 21cc7879e45f69ac309e616cf5b1d55070afb109
1 parent ca5b677 commit f3c802d

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

drawee/src/main/java/com/facebook/drawee/drawable/ScalingUtils.java

+35
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,16 @@ public interface ScaleType {
8686
*/
8787
static final ScaleType FOCUS_CROP = ScaleTypeFocusCrop.INSTANCE;
8888

89+
/**
90+
* Scales the child so that it fits entirely inside the parent. At least one dimension (width or
91+
* height) will fit exactly. Aspect ratio is preserved. Child is aligned to the bottom-left
92+
* corner of the parent.
93+
*/
94+
static final ScaleType FIT_BOTTOM_START = ScaleTypeFitBottomStart.INSTANCE;
95+
8996
/**
9097
* Gets transformation matrix based on the scale type.
98+
*
9199
* @param outTransform out matrix to store result
92100
* @param parentBounds parent bounds
93101
* @param childWidth child width
@@ -212,6 +220,33 @@ public String toString() {
212220
}
213221
}
214222

223+
private static class ScaleTypeFitBottomStart extends AbstractScaleType {
224+
225+
public static final ScaleType INSTANCE = new ScaleTypeFitBottomStart();
226+
227+
@Override
228+
public void getTransformImpl(
229+
Matrix outTransform,
230+
Rect parentRect,
231+
int childWidth,
232+
int childHeight,
233+
float focusX,
234+
float focusY,
235+
float scaleX,
236+
float scaleY) {
237+
float scale = Math.min(scaleX, scaleY);
238+
float dx = parentRect.left;
239+
float dy = parentRect.top + (parentRect.height() - childHeight * scale);
240+
outTransform.setScale(scale, scale);
241+
outTransform.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
242+
}
243+
244+
@Override
245+
public String toString() {
246+
return "fit_bottom_start";
247+
}
248+
}
249+
215250
private static class ScaleTypeFitCenter extends AbstractScaleType {
216251

217252
public static final ScaleType INSTANCE = new ScaleTypeFitCenter();

drawee/src/main/java/com/facebook/drawee/generic/GenericDraweeHierarchyInflater.java

+2
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ private static ScaleType getScaleTypeFromXml(
255255
return ScaleType.CENTER_CROP;
256256
case 7: // focusCrop
257257
return ScaleType.FOCUS_CROP;
258+
case 8: // fitBottomStart
259+
return ScaleType.FIT_BOTTOM_START;
258260
default:
259261
// this method is supposed to be called only when XML attribute is specified.
260262
throw new RuntimeException("XML attribute not specified!");

drawee/src/main/res/values/attrs.xml

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<enum name="centerInside" value="5" />
3131
<enum name="centerCrop" value="6" />
3232
<enum name="focusCrop" value="7" />
33+
<enum name="fitBottomStart" value="8" />
3334
</attr>
3435

3536
<!-- A drawable to be be used as a retry image. -->
@@ -45,6 +46,7 @@
4546
<enum name="centerInside" value="5" />
4647
<enum name="centerCrop" value="6" />
4748
<enum name="focusCrop" value="7" />
49+
<enum name="fitBottomStart" value="8" />
4850
</attr>
4951

5052
<!-- A drawable to be be used as a failure image. -->
@@ -60,6 +62,7 @@
6062
<enum name="centerInside" value="5" />
6163
<enum name="centerCrop" value="6" />
6264
<enum name="focusCrop" value="7" />
65+
<enum name="fitBottomStart" value="8" />
6366
</attr>
6467

6568
<!-- A drawable to be be used as a progress bar. -->
@@ -75,6 +78,7 @@
7578
<enum name="centerInside" value="5" />
7679
<enum name="centerCrop" value="6" />
7780
<enum name="focusCrop" value="7" />
81+
<enum name="fitBottomStart" value="8" />
7882
</attr>
7983
<!-- Progress bar Auto Rotate interval in milliseconds -->
8084
<attr name="progressBarAutoRotateInterval" format="integer"/>
@@ -90,6 +94,7 @@
9094
<enum name="centerInside" value="5" />
9195
<enum name="centerCrop" value="6" />
9296
<enum name="focusCrop" value="7" />
97+
<enum name="fitBottomStart" value="8" />
9398
</attr>
9499

95100
<!-- A drawable or color to be used as a background. -->

drawee/src/test/java/com/facebook/drawee/drawable/ScalingUtilsTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ public void testFocusCrop_FocusBottomRight() {
175175
test(0.80f, 0.80f, 10, -4, 500, 400, 1.00f, 1.00f, ScaleType.FOCUS_CROP);
176176
}
177177

178+
@Test
179+
public void testFitBottomStart() {
180+
test(1.60f, 1.60f, 10, 75, 250, 150, ScaleType.FIT_BOTTOM_START);
181+
test(0.50f, 0.50f, 10, 215, 800, 200, ScaleType.FIT_BOTTOM_START);
182+
test(0.50f, 0.50f, 10, 115, 800, 400, ScaleType.FIT_BOTTOM_START);
183+
test(2.00f, 2.00f, 10, 15, 200, 150, ScaleType.FIT_BOTTOM_START);
184+
test(1.00f, 1.00f, 10, 15, 400, 300, ScaleType.FIT_BOTTOM_START);
185+
test(0.50f, 0.50f, 10, 15, 800, 600, ScaleType.FIT_BOTTOM_START);
186+
test(1.50f, 1.50f, 10, 15, 200, 200, ScaleType.FIT_BOTTOM_START);
187+
test(0.75f, 0.75f, 10, 15, 200, 400, ScaleType.FIT_BOTTOM_START);
188+
test(0.75f, 0.75f, 10, 15, 500, 400, ScaleType.FIT_BOTTOM_START);
189+
}
190+
178191
private void test(
179192
// expected
180193
float scaleX,

0 commit comments

Comments
 (0)