Search in sources :

Example 1 with RoundedBitmapDrawable

use of com.facebook.drawee.drawable.RoundedBitmapDrawable in project fresco by facebook.

the class WrappingUtils method applyLeafRounding.

/**
 * 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(), roundingParams.getRepeatEdgePixels());
        applyRoundingParams(roundedBitmapDrawable, roundingParams);
        return roundedBitmapDrawable;
    } else if (drawable instanceof NinePatchDrawable) {
        final NinePatchDrawable ninePatchDrawableDrawable = (NinePatchDrawable) drawable;
        RoundedNinePatchDrawable roundedNinePatchDrawable = new RoundedNinePatchDrawable(ninePatchDrawableDrawable);
        applyRoundingParams(roundedNinePatchDrawable, roundingParams);
        return roundedNinePatchDrawable;
    } else if (drawable instanceof ColorDrawable) {
        RoundedColorDrawable roundedColorDrawable = RoundedColorDrawable.fromColorDrawable((ColorDrawable) drawable);
        applyRoundingParams(roundedColorDrawable, roundingParams);
        return roundedColorDrawable;
    } else {
        FLog.w(TAG, "Don't know how to round that drawable: %s", drawable);
    }
    return drawable;
}
Also used : RoundedColorDrawable(com.facebook.drawee.drawable.RoundedColorDrawable) RoundedBitmapDrawable(com.facebook.drawee.drawable.RoundedBitmapDrawable) ColorDrawable(android.graphics.drawable.ColorDrawable) RoundedColorDrawable(com.facebook.drawee.drawable.RoundedColorDrawable) RoundedNinePatchDrawable(com.facebook.drawee.drawable.RoundedNinePatchDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) RoundedBitmapDrawable(com.facebook.drawee.drawable.RoundedBitmapDrawable) NinePatchDrawable(android.graphics.drawable.NinePatchDrawable) RoundedNinePatchDrawable(com.facebook.drawee.drawable.RoundedNinePatchDrawable)

Example 2 with RoundedBitmapDrawable

use of com.facebook.drawee.drawable.RoundedBitmapDrawable in project fresco by facebook.

the class BitmapDrawableFactoryTest method testCreateDrawable_whenRoundWithCornerRadius_thenReturnBitmapDrawable.

@Test
public void testCreateDrawable_whenRoundWithCornerRadius_thenReturnBitmapDrawable() {
    final CloseableStaticBitmap closeableImage = mock(CloseableStaticBitmap.class);
    final Bitmap bitmap = mock(Bitmap.class);
    when(closeableImage.getUnderlyingBitmap()).thenReturn(bitmap);
    final ImageOptions options = mock(ImageOptions.class);
    when(options.getRoundingOptions()).thenReturn(RoundingOptions.forCornerRadiusPx(123));
    when(closeableImage.getExtras()).thenReturn(mImageExtrasNotRounded);
    Drawable drawable = mDrawableFactory.createDrawable(closeableImage, options);
    assertThat(drawable).isNotNull();
    assertThat(drawable).isInstanceOf(RoundedBitmapDrawable.class);
    assertThat(((RoundedBitmapDrawable) drawable).getRadii()).isEqualTo(new float[] { 123, 123, 123, 123, 123, 123, 123, 123 });
}
Also used : RoundedBitmapDrawable(com.facebook.drawee.drawable.RoundedBitmapDrawable) Bitmap(android.graphics.Bitmap) CloseableStaticBitmap(com.facebook.imagepipeline.image.CloseableStaticBitmap) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Drawable(android.graphics.drawable.Drawable) RoundedBitmapDrawable(com.facebook.drawee.drawable.RoundedBitmapDrawable) ImageOptions(com.facebook.fresco.vito.options.ImageOptions) CloseableStaticBitmap(com.facebook.imagepipeline.image.CloseableStaticBitmap) Test(org.junit.Test)

Example 3 with RoundedBitmapDrawable

use of com.facebook.drawee.drawable.RoundedBitmapDrawable in project fresco by facebook.

the class RoundingUtilsTest method testRoundedDrawablesWithBorder_withBitmap_withNotAlreadyRounded_thenReturnBitmapDrawable.

@Test
public void testRoundedDrawablesWithBorder_withBitmap_withNotAlreadyRounded_thenReturnBitmapDrawable() {
    RoundingUtils roundingUtils = new RoundingUtils();
    final Bitmap bitmap = mock(Bitmap.class);
    BorderOptions borderOptions = BorderOptions.create(Color.YELLOW, 10);
    Drawable drawable = roundingUtils.roundedDrawable(mResources, bitmap, borderOptions, RoundingOptions.asCircle());
    assertThat(drawable).isNotNull();
    assertThat(drawable).isInstanceOf(RoundedBitmapDrawable.class);
    assertThat(((RoundedBitmapDrawable) drawable).getBorderWidth()).isEqualTo(borderOptions.width);
    assertThat(((RoundedBitmapDrawable) drawable).getBorderColor()).isEqualTo(borderOptions.color);
    assertThat(((RoundedBitmapDrawable) drawable).isCircle()).isTrue();
}
Also used : RoundedBitmapDrawable(com.facebook.drawee.drawable.RoundedBitmapDrawable) Bitmap(android.graphics.Bitmap) NinePatchDrawable(android.graphics.drawable.NinePatchDrawable) ColorDrawable(android.graphics.drawable.ColorDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Drawable(android.graphics.drawable.Drawable) RoundedNinePatchDrawable(com.facebook.drawee.drawable.RoundedNinePatchDrawable) RoundedColorDrawable(com.facebook.drawee.drawable.RoundedColorDrawable) RoundedBitmapDrawable(com.facebook.drawee.drawable.RoundedBitmapDrawable) BorderOptions(com.facebook.fresco.vito.options.BorderOptions) Test(org.junit.Test)

Example 4 with RoundedBitmapDrawable

use of com.facebook.drawee.drawable.RoundedBitmapDrawable in project fresco by facebook.

the class HierarcherImplTest method testApplyRoundingOptions_whenRoundWithCornerRadius_thenReturnDrawable.

@Test
public void testApplyRoundingOptions_whenRoundWithCornerRadius_thenReturnDrawable() {
    final BitmapDrawable drawable = mock(BitmapDrawable.class);
    final Bitmap bitmap = mock(Bitmap.class);
    when(drawable.getBitmap()).thenReturn(bitmap);
    ImageOptions options = mock(ImageOptions.class);
    when(options.getRoundingOptions()).thenReturn(RoundingOptions.forCornerRadiusPx(123));
    when(options.getPlaceholderDrawable()).thenReturn(drawable);
    when(options.getPlaceholderApplyRoundingOptions()).thenReturn(true);
    Drawable result = mHierarcher.buildPlaceholderDrawable(mResources, options);
    assertThat(result).isNotNull();
    assertThat(result).isInstanceOf(RoundedBitmapDrawable.class);
    assertThat(((RoundedBitmapDrawable) result).getRadii()).isEqualTo(new float[] { 123, 123, 123, 123, 123, 123, 123, 123 });
}
Also used : RoundedBitmapDrawable(com.facebook.drawee.drawable.RoundedBitmapDrawable) Bitmap(android.graphics.Bitmap) ColorDrawable(android.graphics.drawable.ColorDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Drawable(android.graphics.drawable.Drawable) ScaleTypeDrawable(com.facebook.drawee.drawable.ScaleTypeDrawable) RoundedColorDrawable(com.facebook.drawee.drawable.RoundedColorDrawable) RoundedBitmapDrawable(com.facebook.drawee.drawable.RoundedBitmapDrawable) ImageOptions(com.facebook.fresco.vito.options.ImageOptions) BitmapDrawable(android.graphics.drawable.BitmapDrawable) RoundedBitmapDrawable(com.facebook.drawee.drawable.RoundedBitmapDrawable) Test(org.junit.Test)

Example 5 with RoundedBitmapDrawable

use of com.facebook.drawee.drawable.RoundedBitmapDrawable in project fresco by facebook.

the class HierarcherImplTest method testApplyRoundingOptions_whenRoundWithCornerRadii_thenReturnDrawable.

@Test
public void testApplyRoundingOptions_whenRoundWithCornerRadii_thenReturnDrawable() {
    final BitmapDrawable drawable = mock(BitmapDrawable.class);
    final Bitmap bitmap = mock(Bitmap.class);
    when(drawable.getBitmap()).thenReturn(bitmap);
    ImageOptions options = mock(ImageOptions.class);
    when(options.getRoundingOptions()).thenReturn(RoundingOptions.forCornerRadii(1, 2, 3, 4));
    when(options.getPlaceholderDrawable()).thenReturn(drawable);
    when(options.getPlaceholderApplyRoundingOptions()).thenReturn(true);
    Drawable result = mHierarcher.buildPlaceholderDrawable(mResources, options);
    assertThat(result).isNotNull();
    assertThat(result).isInstanceOf(RoundedBitmapDrawable.class);
    assertThat(((RoundedBitmapDrawable) result).getRadii()).isEqualTo(new float[] { 1, 1, 2, 2, 3, 3, 4, 4 });
}
Also used : RoundedBitmapDrawable(com.facebook.drawee.drawable.RoundedBitmapDrawable) Bitmap(android.graphics.Bitmap) ColorDrawable(android.graphics.drawable.ColorDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Drawable(android.graphics.drawable.Drawable) ScaleTypeDrawable(com.facebook.drawee.drawable.ScaleTypeDrawable) RoundedColorDrawable(com.facebook.drawee.drawable.RoundedColorDrawable) RoundedBitmapDrawable(com.facebook.drawee.drawable.RoundedBitmapDrawable) ImageOptions(com.facebook.fresco.vito.options.ImageOptions) BitmapDrawable(android.graphics.drawable.BitmapDrawable) RoundedBitmapDrawable(com.facebook.drawee.drawable.RoundedBitmapDrawable) Test(org.junit.Test)

Aggregations

BitmapDrawable (android.graphics.drawable.BitmapDrawable)9 RoundedBitmapDrawable (com.facebook.drawee.drawable.RoundedBitmapDrawable)9 Drawable (android.graphics.drawable.Drawable)8 Test (org.junit.Test)8 ColorDrawable (android.graphics.drawable.ColorDrawable)7 RoundedColorDrawable (com.facebook.drawee.drawable.RoundedColorDrawable)7 Bitmap (android.graphics.Bitmap)6 NinePatchDrawable (android.graphics.drawable.NinePatchDrawable)5 RoundedNinePatchDrawable (com.facebook.drawee.drawable.RoundedNinePatchDrawable)5 ImageOptions (com.facebook.fresco.vito.options.ImageOptions)4 ScaleTypeDrawable (com.facebook.drawee.drawable.ScaleTypeDrawable)2 BorderOptions (com.facebook.fresco.vito.options.BorderOptions)2 RoundingOptions (com.facebook.fresco.vito.options.RoundingOptions)2 CloseableStaticBitmap (com.facebook.imagepipeline.image.CloseableStaticBitmap)2