4242import androidx .annotation .NonNull ;
4343import androidx .annotation .Nullable ;
4444import androidx .annotation .Px ;
45- import androidx .annotation .RequiresApi ;
4645import androidx .annotation .RestrictTo ;
4746import androidx .annotation .RestrictTo .Scope ;
4847import androidx .annotation .XmlRes ;
@@ -64,7 +63,7 @@ public final class DrawableUtils {
6463
6564 /**
6665 * Indicates to use the intrinsic size of the {@link Drawable}.
67- *
66+ *
6867 * <p>Used in {@link #compositeTwoLayeredDrawable(Drawable, Drawable, int, int)}.
6968 */
7069 public static final int INTRINSIC_SIZE = -1 ;
@@ -211,7 +210,7 @@ private static Drawable createTintableMutatedDrawableIfNeeded(
211210 /**
212211 * Composites two drawables, returning a drawable instance of {@link LayerDrawable},
213212 * with the top layer centered.
214- *
213+ *
215214 * <p>If any of the drawables is null, this method will return the other.
216215 *
217216 * @param bottomLayerDrawable the drawable to be on the bottom layer
@@ -221,35 +220,16 @@ private static Drawable createTintableMutatedDrawableIfNeeded(
221220 public static Drawable compositeTwoLayeredDrawable (
222221 @ Nullable Drawable bottomLayerDrawable ,
223222 @ Nullable Drawable topLayerDrawable ) {
224- if (VERSION .SDK_INT >= VERSION_CODES .M ) {
225- return compositeTwoLayeredDrawable (
226- bottomLayerDrawable , topLayerDrawable , INTRINSIC_SIZE , INTRINSIC_SIZE );
227- }
228-
229- if (bottomLayerDrawable == null ) {
230- return topLayerDrawable ;
231- }
232- if (topLayerDrawable == null ) {
233- return bottomLayerDrawable ;
234- }
235- LayerDrawable drawable =
236- new LayerDrawable (new Drawable [] {bottomLayerDrawable , topLayerDrawable });
237- int horizontalInset =
238- max (bottomLayerDrawable .getIntrinsicWidth ()
239- - getTopLayerIntrinsicWidth (bottomLayerDrawable , topLayerDrawable ), 0 ) / 2 ;
240- int verticalInset =
241- max (bottomLayerDrawable .getIntrinsicHeight ()
242- - getTopLayerIntrinsicHeight (bottomLayerDrawable , topLayerDrawable ), 0 ) / 2 ;
243- drawable .setLayerInset (1 , horizontalInset , verticalInset , horizontalInset , verticalInset );
244- return drawable ;
223+ return compositeTwoLayeredDrawable (
224+ bottomLayerDrawable , topLayerDrawable , INTRINSIC_SIZE , INTRINSIC_SIZE );
245225 }
246226
247227 /**
248228 * Composites two drawables, returning a drawable instance of {@link LayerDrawable},
249229 * with the top layer centered to the bottom layer. The top layer will be scaled according to the
250230 * provided desired width/height and the size of the bottom layer so the top layer can fit in the
251231 * bottom layer and preserve its desired aspect ratio.
252- *
232+ *
253233 * <p>If any of the drawables is null, this method will return the other.
254234 *
255235 * @param bottomLayerDrawable the drawable to be on the bottom layer
@@ -259,7 +239,6 @@ public static Drawable compositeTwoLayeredDrawable(
259239 * @param topLayerDesiredHeight top layer desired height in pixels, or {@link #INTRINSIC_SIZE} to
260240 * use the intrinsic height.
261241 */
262- @ RequiresApi (VERSION_CODES .M )
263242 @ Nullable
264243 public static Drawable compositeTwoLayeredDrawable (
265244 @ Nullable Drawable bottomLayerDrawable ,
@@ -272,8 +251,6 @@ public static Drawable compositeTwoLayeredDrawable(
272251 if (topLayerDrawable == null ) {
273252 return bottomLayerDrawable ;
274253 }
275- LayerDrawable drawable =
276- new LayerDrawable (new Drawable [] {bottomLayerDrawable , topLayerDrawable });
277254
278255 if (topLayerDesiredWidth == INTRINSIC_SIZE ) {
279256 topLayerDesiredWidth = getTopLayerIntrinsicWidth (bottomLayerDrawable , topLayerDrawable );
@@ -308,23 +285,41 @@ public static Drawable compositeTwoLayeredDrawable(
308285 }
309286 }
310287
311- drawable .setLayerSize (1 , topLayerNewWidth , topLayerNewHeight );
312- drawable .setLayerGravity (1 , Gravity .CENTER );
288+ LayerDrawable drawable ;
289+ if (VERSION .SDK_INT >= VERSION_CODES .M ) {
290+ drawable = new LayerDrawable (new Drawable [] {bottomLayerDrawable , topLayerDrawable });
291+
292+ drawable .setLayerSize (1 , topLayerNewWidth , topLayerNewHeight );
293+ drawable .setLayerGravity (1 , Gravity .CENTER );
294+ } else {
295+ Drawable scaledTopLayerDrawable =
296+ new ScaledDrawableWrapper (topLayerDrawable , topLayerNewWidth , topLayerNewHeight )
297+ .getDrawable ();
298+
299+ drawable = new LayerDrawable (new Drawable [] {bottomLayerDrawable , scaledTopLayerDrawable });
300+
301+ final int horizontalInset =
302+ max ((bottomLayerDrawable .getIntrinsicWidth () - topLayerNewWidth ) / 2 , 0 );
303+ final int verticalInset =
304+ max ((bottomLayerDrawable .getIntrinsicHeight () - topLayerNewHeight ) / 2 , 0 );
305+ drawable .setLayerInset (1 , horizontalInset , verticalInset , horizontalInset , verticalInset );
306+ }
307+
313308 return drawable ;
314309 }
315310
316311 private static int getTopLayerIntrinsicWidth (
317312 @ NonNull Drawable bottomLayerDrawable , @ NonNull Drawable topLayerDrawable ) {
318313 int topLayerIntrinsicWidth = topLayerDrawable .getIntrinsicWidth ();
319- return topLayerIntrinsicWidth = = UNSPECIFIED_WIDTH
320- ? bottomLayerDrawable .getIntrinsicWidth () : topLayerIntrinsicWidth ;
314+ return topLayerIntrinsicWidth ! = UNSPECIFIED_WIDTH
315+ ? topLayerIntrinsicWidth : bottomLayerDrawable .getIntrinsicWidth ();
321316 }
322317
323318 private static int getTopLayerIntrinsicHeight (
324319 @ NonNull Drawable bottomLayerDrawable , @ NonNull Drawable topLayerDrawable ) {
325320 int topLayerIntrinsicHeight = topLayerDrawable .getIntrinsicHeight ();
326- return topLayerIntrinsicHeight = = UNSPECIFIED_HEIGHT
327- ? bottomLayerDrawable .getIntrinsicHeight () : topLayerIntrinsicHeight ;
321+ return topLayerIntrinsicHeight ! = UNSPECIFIED_HEIGHT
322+ ? topLayerIntrinsicHeight : bottomLayerDrawable .getIntrinsicHeight ();
328323 }
329324
330325 /** Returns a new state that adds the checked state to the input state. */
0 commit comments