/** * Rounds the given drawable with a {@link RoundedBitmapDrawable} or {@link RoundedColorDrawable}. * * <p> If the given drawable is not a {@link BitmapDrawable} or a {@link ColorDrawable}, it is * returned without being rounded. * * @return the rounded drawable, or the original drawable if rounding didn't take place */ private static Drawable applyLeafRounding( Drawable drawable, RoundingParams roundingParams, Resources resources) { if (drawable instanceof BitmapDrawable) { final BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; RoundedBitmapDrawable roundedBitmapDrawable = new RoundedBitmapDrawable( resources, bitmapDrawable.getBitmap(), bitmapDrawable.getPaint()); applyRoundingParams(roundedBitmapDrawable, roundingParams); return roundedBitmapDrawable; } if (drawable instanceof ColorDrawable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { RoundedColorDrawable roundedColorDrawable = RoundedColorDrawable.fromColorDrawable((ColorDrawable) drawable); applyRoundingParams(roundedColorDrawable, roundingParams); return roundedColorDrawable; } return drawable; }
public static Drawable applyRounding( @Nullable RoundingParams roundingParams, Resources resources, Drawable drawable) { if (drawable instanceof BitmapDrawable) { RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawable.fromBitmapDrawable(resources, (BitmapDrawable) drawable); applyRoundingParams(roundedBitmapDrawable, roundingParams); return roundedBitmapDrawable; } if (drawable instanceof ColorDrawable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { RoundedColorDrawable roundedColorDrawable = RoundedColorDrawable.fromColorDrawable((ColorDrawable) drawable); applyRoundingParams(roundedColorDrawable, roundingParams); return roundedColorDrawable; } return drawable; }
@Test public void testControlling_WithCornerRadii() throws Exception { GenericDraweeHierarchy dh = mBuilder .setPlaceholderImage(mPlaceholderImage, null) .setActualImageScaleType(null) .setRoundingParams(RoundingParams.fromCornersRadius(10)) .setFadeDuration(250) .build(); // actual image index in DH tree final int imageIndex = 2; FadeDrawable fadeDrawable = (FadeDrawable) dh.getTopLevelDrawable().getCurrent(); ForwardingDrawable settableDrawable = (ForwardingDrawable) fadeDrawable.getDrawable(imageIndex); // set temporary image dh.setImage(mActualImage1, 0.5f, true); assertNotSame(mActualImage1, settableDrawable.getCurrent()); assertEquals(RoundedBitmapDrawable.class, settableDrawable.getCurrent().getClass()); assertEquals(true, fadeDrawable.isLayerOn(imageIndex)); assertEquals(FadeDrawable.TRANSITION_NONE, fadeDrawable.getTransitionState()); verifyCallback(dh.getTopLevelDrawable(), settableDrawable.getCurrent()); // set final image dh.setImage(mActualImage2, 1f, false); assertNotSame(mActualImage2, settableDrawable.getCurrent()); assertEquals(RoundedBitmapDrawable.class, settableDrawable.getCurrent().getClass()); assertEquals(true, fadeDrawable.isLayerOn(imageIndex)); assertEquals(FadeDrawable.TRANSITION_STARTING, fadeDrawable.getTransitionState()); assertEquals(250, fadeDrawable.getTransitionDuration()); verifyCallback(dh.getTopLevelDrawable(), settableDrawable.getCurrent()); }