Skip to content

Commit

Permalink
Adding rounding support for placeholders
Browse files Browse the repository at this point in the history
Reviewed By: oprisnik

Differential Revision: D18370836

fbshipit-source-id: 79477a563f4a2000a58953c66079171452a96fc7
  • Loading branch information
nicous authored and facebook-github-bot committed Nov 8, 2019
1 parent 94df9e2 commit e0f8a73
Show file tree
Hide file tree
Showing 8 changed files with 527 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import kotlinx.android.synthetic.main.fragment_vito_image_options_config.*
/** Experimental Fresco Vito fragment that allows to configure ImageOptions via a simple UI. */
class FrescoVitoLithoImageOptionsConfigFragment : BaseShowcaseFragment() {

private val imageOptionsBuilder = ImageOptions.create().autoPlay(true)
private val imageOptionsBuilder = ImageOptions.create().autoPlay(true).placeholderApplyRoundingOptions(true)

private var currentUri: Uri? = null
private var componentContext: ComponentContext? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@
import com.facebook.common.references.CloseableReference;
import com.facebook.drawee.drawable.ForwardingDrawable;
import com.facebook.drawee.drawable.ScaleTypeDrawable;
import com.facebook.fresco.vito.drawable.RoundingUtils;
import com.facebook.fresco.vito.drawable.VitoDrawableFactory;
import com.facebook.fresco.vito.options.BorderOptions;
import com.facebook.fresco.vito.options.ImageOptions;
import com.facebook.fresco.vito.options.RoundingOptions;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.systrace.FrescoSystrace;

public class HierarcherImpl implements Hierarcher {
private static final Drawable NOP_DRAWABLE = NopDrawable.INSTANCE;

private final VitoDrawableFactory mDrawableFactory;
private final RoundingUtils mRoundingUtils;

public HierarcherImpl(VitoDrawableFactory drawableFactory) {
mDrawableFactory = drawableFactory;
mRoundingUtils = new RoundingUtils();
}

@Override
Expand All @@ -42,6 +47,9 @@ public Drawable buildPlaceholderDrawable(Resources resources, ImageOptions image
if (placeholderDrawable == null) {
return NOP_DRAWABLE;
}
if (imageOptions.getPlaceholderApplyRoundingOptions()) {
placeholderDrawable = applyRoundingOptions(resources, placeholderDrawable, imageOptions);
}
if (imageOptions.getPlaceholderScaleType() != null) {
return new ScaleTypeDrawable(placeholderDrawable, imageOptions.getPlaceholderScaleType());
}
Expand All @@ -53,6 +61,15 @@ public Drawable buildPlaceholderDrawable(Resources resources, ImageOptions image
}
}

protected Drawable applyRoundingOptions(
Resources resources, Drawable placeholderDrawable, ImageOptions imageOptions) {
RoundingOptions roundingOptions = imageOptions.getRoundingOptions();
BorderOptions borderOptions = imageOptions.getBorderOptions();

return mRoundingUtils.roundedDrawable(
resources, placeholderDrawable, borderOptions, roundingOptions);
}

@Override
public Drawable buildProgressDrawable(Resources resources, ImageOptions imageOptions) {
if (FrescoSystrace.isTracing()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
package com.facebook.fresco.vito.drawable;

import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import com.facebook.drawee.drawable.OrientedDrawable;
import com.facebook.drawee.drawable.RoundedBitmapDrawable;
import com.facebook.fresco.vito.core.FrescoExperiments;
import com.facebook.fresco.vito.options.BorderOptions;
import com.facebook.fresco.vito.options.ImageOptions;
Expand All @@ -27,10 +25,12 @@ public class BitmapDrawableFactory implements VitoDrawableFactory {

private final Resources mResources;
private final FrescoExperiments mExperiments;
private final RoundingUtils mRoundingUtils;

public BitmapDrawableFactory(Resources resources, FrescoExperiments frescoExperiments) {
mResources = resources;
mExperiments = frescoExperiments;
mRoundingUtils = new RoundingUtils();
}

@Override
Expand Down Expand Up @@ -72,98 +72,15 @@ protected Drawable handleCloseableStaticBitmap(
CloseableStaticBitmap closeableStaticBitmap, ImageOptions imageOptions) {
RoundingOptions roundingOptions = imageOptions.getRoundingOptions();
BorderOptions borderOptions = imageOptions.getBorderOptions();

if (borderOptions != null && borderOptions.width > 0) {
return rotatedDrawable(
closeableStaticBitmap,
roundedDrawableWithBorder(closeableStaticBitmap, borderOptions, roundingOptions));
} else {
return rotatedDrawable(
closeableStaticBitmap,
roundedDrawableWithoutBorder(closeableStaticBitmap, roundingOptions));
}
}

protected Drawable roundedDrawableWithoutBorder(
CloseableStaticBitmap closeableStaticBitmap, @Nullable RoundingOptions roundingOptions) {
// We need to return a rounded corner drawable if needed
if (roundingOptions != null && !roundingOptions.isCircular()) {
return roundedCornerDrawableWithBorder(closeableStaticBitmap, null, roundingOptions);
}
if (!mExperiments.useNativeRounding()
&& roundingOptions != null
&& roundingOptions.isCircular()) {
return circularAtRenderTimeDrawable(closeableStaticBitmap, null);
}
return new BitmapDrawable(mResources, closeableStaticBitmap.getUnderlyingBitmap());
}

protected Drawable roundedDrawableWithBorder(
CloseableStaticBitmap closeableStaticBitmap,
BorderOptions borderOptions,
@Nullable RoundingOptions roundingOptions) {
if (roundingOptions != null) {
if (roundingOptions.isCircular()) {
if (mExperiments.useNativeRounding()) {
// Circular rounding is performed on the bitmap, so we only need to draw a circular border
return circularNativeDrawableWithBorder(closeableStaticBitmap, borderOptions);
}
return circularAtRenderTimeDrawable(closeableStaticBitmap, borderOptions);
} else {
return roundedCornerDrawableWithBorder(
closeableStaticBitmap, borderOptions, roundingOptions);
}
} else {
return squareDrawableWithBorder(closeableStaticBitmap, borderOptions);
}
}

protected Drawable circularNativeDrawableWithBorder(
CloseableStaticBitmap closeableStaticBitmap, BorderOptions borderOptions) {
CircularBorderBitmapDrawable drawable =
new CircularBorderBitmapDrawable(mResources, closeableStaticBitmap.getUnderlyingBitmap());
drawable.setBorder(borderOptions);
return drawable;
}

protected Drawable circularAtRenderTimeDrawable(
CloseableStaticBitmap closeableStaticBitmap, @Nullable BorderOptions borderOptions) {
RoundedBitmapDrawable roundedBitmapDrawable =
new RoundedBitmapDrawable(mResources, closeableStaticBitmap.getUnderlyingBitmap());

roundedBitmapDrawable.setCircle(true);

if (borderOptions != null) {
roundedBitmapDrawable.setBorder(borderOptions.color, borderOptions.width);
}
return roundedBitmapDrawable;
}

protected Drawable roundedCornerDrawableWithBorder(
CloseableStaticBitmap closeableStaticBitmap,
@Nullable BorderOptions borderOptions,
@Nullable RoundingOptions roundingOptions) {
RoundedBitmapDrawable roundedBitmapDrawable =
new RoundedBitmapDrawable(mResources, closeableStaticBitmap.getUnderlyingBitmap());
if (roundingOptions != null) {
float[] radii = roundingOptions.getCornerRadii();
if (radii != null) {
roundedBitmapDrawable.setRadii(radii);
} else {
roundedBitmapDrawable.setRadius(roundingOptions.getCornerRadius());
}
}
if (borderOptions != null) {
roundedBitmapDrawable.setBorder(borderOptions.color, borderOptions.width);
roundedBitmapDrawable.setPadding(borderOptions.padding);
}
return roundedBitmapDrawable;
}

protected Drawable squareDrawableWithBorder(
CloseableStaticBitmap closeableStaticBitmap, BorderOptions borderOptions) {
// We use the same rounded corner drawable to draw the border without applying rounding
return roundedCornerDrawableWithBorder(closeableStaticBitmap, borderOptions, null);
mRoundingUtils.setAlreadyRounded(mExperiments.useNativeRounding());

return rotatedDrawable(
closeableStaticBitmap,
mRoundingUtils.roundedDrawable(
mResources,
closeableStaticBitmap.getUnderlyingBitmap(),
borderOptions,
roundingOptions));
}

protected Drawable rotatedDrawable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public void setBorder(@Nullable BorderOptions borderOptions) {
}
}

public @Nullable BorderOptions getBorder() {
return mBorderOptions;
}

@Override
public void setAlpha(int alpha) {
super.setAlpha(alpha);
Expand Down
Loading

0 comments on commit e0f8a73

Please sign in to comment.