com.facebook.drawee.drawable.ScaleTypeDrawable Java Examples

The following examples show how to use com.facebook.drawee.drawable.ScaleTypeDrawable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HierarcherImplTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testBuildPlaceholderRes() {
  ImageOptions options = ImageOptions.create().placeholderRes(RES_ID).build();

  Drawable errorDrawable = mHierarcher.buildPlaceholderDrawable(mResources, options);

  assertThat(errorDrawable).isNotNull();
  assertThat(errorDrawable).isInstanceOf(ScaleTypeDrawable.class);

  ScaleTypeDrawable scaleTypeDrawable = (ScaleTypeDrawable) errorDrawable;
  assertThat(scaleTypeDrawable.getScaleType())
      .isEqualTo(ImageOptions.defaults().getPlaceholderScaleType());
  assertThat(scaleTypeDrawable.getFocusPoint())
      .isEqualTo(ImageOptions.defaults().getPlaceholderFocusPoint());
  assertThat(scaleTypeDrawable.getCurrent()).isEqualTo(mDrawable);
}
 
Example #2
Source File: GenericDraweeHierarchyTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testSetActualImageScaleType() {
  GenericDraweeHierarchy dh = mBuilder.setPlaceholderImage(mPlaceholderImage).build();

  // actual image index in DH tree
  final int imageIndex = 2;

  FadeDrawable fadeDrawable = (FadeDrawable) dh.getTopLevelDrawable().getCurrent();
  ScaleTypeDrawable scaleTypeDrawable = (ScaleTypeDrawable) fadeDrawable.getDrawable(imageIndex);

  ScaleType scaleType1 = ScaleType.FOCUS_CROP;
  dh.setActualImageScaleType(scaleType1);
  assertEquals(scaleType1, scaleTypeDrawable.getScaleType());

  ScaleType scaleType2 = ScaleType.CENTER;
  dh.setActualImageScaleType(scaleType2);
  assertEquals(scaleType2, scaleTypeDrawable.getScaleType());
}
 
Example #3
Source File: GenericDraweeHierarchyTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testSetActualImageFocusPoint() {
  GenericDraweeHierarchy dh =
      mBuilder
          .setPlaceholderImage(mPlaceholderImage)
          .setProgressBarImage(mProgressBarImage)
          .setActualImageScaleType(ScaleType.FOCUS_CROP)
          .build();

  // actual image index in DH tree
  final int imageIndex = 2;

  FadeDrawable fadeDrawable = (FadeDrawable) dh.getTopLevelDrawable().getCurrent();
  ScaleTypeDrawable scaleTypeDrawable = (ScaleTypeDrawable) fadeDrawable.getDrawable(imageIndex);
  assertNull(scaleTypeDrawable.getFocusPoint());

  PointF focus1 = new PointF(0.3f, 0.4f);
  dh.setActualImageFocusPoint(focus1);
  AndroidGraphicsTestUtils.assertEquals(focus1, scaleTypeDrawable.getFocusPoint(), 0f);

  PointF focus2 = new PointF(0.6f, 0.7f);
  dh.setActualImageFocusPoint(focus2);
  AndroidGraphicsTestUtils.assertEquals(focus2, scaleTypeDrawable.getFocusPoint(), 0f);
}
 
Example #4
Source File: HierarcherImpl.java    From fresco with MIT License 6 votes vote down vote up
@Override
@Nullable
public Drawable buildErrorDrawable(Resources resources, ImageOptions imageOptions) {
  if (FrescoSystrace.isTracing()) {
    FrescoSystrace.beginSection("HierarcherImpl#buildErrorDrawable");
  }
  try {
    if (imageOptions.getErrorRes() == 0) {
      return null;
    }
    Drawable drawable = resources.getDrawable(imageOptions.getErrorRes());
    if (imageOptions.getErrorScaleType() != null) {
      return new ScaleTypeDrawable(
          drawable, imageOptions.getErrorScaleType(), imageOptions.getErrorFocusPoint());
    }
    return drawable;
  } finally {
    if (FrescoSystrace.isTracing()) {
      FrescoSystrace.endSection();
    }
  }
}
 
Example #5
Source File: HierarcherImpl.java    From fresco with MIT License 6 votes vote down vote up
@Override
public Drawable buildProgressDrawable(Resources resources, ImageOptions imageOptions) {
  if (FrescoSystrace.isTracing()) {
    FrescoSystrace.beginSection("HierarcherImpl#buildProgressDrawable");
  }
  try {
    if (imageOptions.getProgressRes() == 0 && imageOptions.getProgressDrawable() == null) {
      return NOP_DRAWABLE;
    }
    Drawable progressDrawable = imageOptions.getProgressDrawable();
    if (progressDrawable == null) {
      progressDrawable = resources.getDrawable(imageOptions.getProgressRes());
    }
    if (imageOptions.getProgressScaleType() != null) {
      return new ScaleTypeDrawable(progressDrawable, imageOptions.getProgressScaleType());
    }
    return progressDrawable;
  } finally {
    if (FrescoSystrace.isTracing()) {
      FrescoSystrace.endSection();
    }
  }
}
 
Example #6
Source File: WrappingUtils.java    From fresco with MIT License 6 votes vote down vote up
/**
 * Wraps the given drawable with a new {@link ScaleTypeDrawable}.
 *
 * <p>If the provided drawable or scale type is null, the given drawable is returned without being
 * wrapped.
 *
 * @return the wrapping scale type drawable, or the original drawable if the wrapping didn't take
 *     place
 */
@Nullable
static Drawable maybeWrapWithScaleType(
    @Nullable Drawable drawable,
    @Nullable ScalingUtils.ScaleType scaleType,
    @Nullable PointF focusPoint) {
  if (FrescoSystrace.isTracing()) {
    FrescoSystrace.beginSection("WrappingUtils#maybeWrapWithScaleType");
  }
  if (drawable == null || scaleType == null) {
    if (FrescoSystrace.isTracing()) {
      FrescoSystrace.endSection();
    }
    return drawable;
  }
  ScaleTypeDrawable scaleTypeDrawable = new ScaleTypeDrawable(drawable, scaleType);
  if (focusPoint != null) {
    scaleTypeDrawable.setFocusPoint(focusPoint);
  }
  if (FrescoSystrace.isTracing()) {
    FrescoSystrace.endSection();
  }
  return scaleTypeDrawable;
}
 
Example #7
Source File: HierarcherImpl.java    From fresco with MIT License 6 votes vote down vote up
@Override
public Drawable buildPlaceholderDrawable(Resources resources, ImageOptions imageOptions) {
  if (FrescoSystrace.isTracing()) {
    FrescoSystrace.beginSection("HierarcherImpl#buildPlaceholderDrawable");
  }
  try {
    @Nullable Drawable placeholderDrawable = imageOptions.getPlaceholderDrawable();
    if (placeholderDrawable == null && imageOptions.getPlaceholderRes() != 0) {
      placeholderDrawable = resources.getDrawable(imageOptions.getPlaceholderRes());
    }
    if (placeholderDrawable == null) {
      return NOP_DRAWABLE;
    }
    if (imageOptions.getPlaceholderApplyRoundingOptions()) {
      placeholderDrawable = applyRoundingOptions(resources, placeholderDrawable, imageOptions);
    }
    if (imageOptions.getPlaceholderScaleType() != null) {
      return new ScaleTypeDrawable(placeholderDrawable, imageOptions.getPlaceholderScaleType());
    }
    return placeholderDrawable;
  } finally {
    if (FrescoSystrace.isTracing()) {
      FrescoSystrace.endSection();
    }
  }
}
 
Example #8
Source File: HierarcherImplTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testBuildErrorDrawable_whenNoScaleTypeSet_thenUseDefaultScaleType() {
  ImageOptions options = ImageOptions.create().errorRes(RES_ID).build();

  Drawable errorDrawable = mHierarcher.buildErrorDrawable(mResources, options);

  assertThat(errorDrawable).isNotNull();
  assertThat(errorDrawable).isInstanceOf(ScaleTypeDrawable.class);

  ScaleTypeDrawable scaleTypeDrawable = (ScaleTypeDrawable) errorDrawable;
  assertThat(scaleTypeDrawable.getScaleType())
      .isEqualTo(ImageOptions.defaults().getErrorScaleType());
  assertThat(scaleTypeDrawable.getFocusPoint())
      .isEqualTo(ImageOptions.defaults().getErrorFocusPoint());
  assertThat(scaleTypeDrawable.getCurrent()).isEqualTo(mDrawable);
}
 
Example #9
Source File: HierarcherImplTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testBuildErrorDrawable_whenScaleTypeSet_thenReturnScaleTypeDrawable() {
  PointF focusPoint = new PointF(100, 234);
  ImageOptions options =
      ImageOptions.create()
          .errorRes(RES_ID)
          .errorScaleType(ScalingUtils.ScaleType.FOCUS_CROP)
          .errorFocusPoint(focusPoint)
          .build();

  Drawable errorDrawable = mHierarcher.buildErrorDrawable(mResources, options);

  assertThat(errorDrawable).isNotNull();
  assertThat(errorDrawable).isInstanceOf(ScaleTypeDrawable.class);

  ScaleTypeDrawable scaleTypeDrawable = (ScaleTypeDrawable) errorDrawable;
  assertThat(scaleTypeDrawable.getScaleType()).isEqualTo(ScalingUtils.ScaleType.FOCUS_CROP);
  assertThat(scaleTypeDrawable.getFocusPoint()).isEqualTo(focusPoint);
  assertThat(scaleTypeDrawable.getCurrent()).isEqualTo(mDrawable);
}
 
Example #10
Source File: HierarcherImplTest.java    From fresco with MIT License 5 votes vote down vote up
@Test
public void testBuildActualImageWrapper() {
  final PointF expectedFocusPoint = new PointF(1, 2);
  final ImageOptions imageOptions =
      ImageOptions.create()
          .scale(ScalingUtils.ScaleType.FIT_CENTER)
          .focusPoint(expectedFocusPoint)
          .build();

  final Drawable actual = mHierarcher.buildActualImageWrapper(imageOptions);
  assertThat(actual).isInstanceOf(ScaleTypeDrawable.class);
  final ScaleTypeDrawable scaleTypeActual = (ScaleTypeDrawable) actual;
  assertThat(scaleTypeActual.getScaleType()).isEqualTo(ScalingUtils.ScaleType.FIT_CENTER);
  assertThat(scaleTypeActual.getFocusPoint()).isEqualTo(expectedFocusPoint);
}
 
Example #11
Source File: HierarcherImplTest.java    From fresco with MIT License 5 votes vote down vote up
@Test
public void testBuildPlaceholderDrawableScale() {
  final Drawable expected = new ColorDrawable(Color.YELLOW);
  ImageOptions options =
      ImageOptions.create()
          .placeholder(expected)
          .placeholderScaleType(ScalingUtils.ScaleType.CENTER)
          .build();

  final Drawable result = mHierarcher.buildPlaceholderDrawable(mResources, options);

  assertThat(result).isExactlyInstanceOf(ScaleTypeDrawable.class);
  assertThat(result.getCurrent()).isEqualTo(expected);
}
 
Example #12
Source File: HierarcherImplTest.java    From fresco with MIT License 5 votes vote down vote up
@Test
public void testBuildProgressDrawable() {
  final Drawable drawable = new ColorDrawable(0x0);
  final ImageOptions imageOptions =
      ImageOptions.create()
          .progress(drawable)
          .progressScaleType(ScalingUtils.ScaleType.FIT_CENTER)
          .build();

  final Drawable actual = mHierarcher.buildProgressDrawable(mResources, imageOptions);
  assertThat(actual).isInstanceOf(ScaleTypeDrawable.class);
  final ScaleTypeDrawable scaleTypeActual = (ScaleTypeDrawable) actual;
  assertThat(scaleTypeActual.getScaleType()).isEqualTo(ScalingUtils.ScaleType.FIT_CENTER);
  assertThat(scaleTypeActual.getCurrent()).isEqualTo(drawable);
}
 
Example #13
Source File: HierarcherImpl.java    From fresco with MIT License 5 votes vote down vote up
@Override
public ForwardingDrawable buildActualImageWrapper(ImageOptions imageOptions) {
  ScaleTypeDrawable wrapper =
      new ScaleTypeDrawable(
          NOP_DRAWABLE,
          imageOptions.getActualImageScaleType(),
          imageOptions.getActualImageFocusPoint());
  ColorFilter actualImageColorFilter = imageOptions.getActualImageColorFilter();
  if (actualImageColorFilter != null) {
    wrapper.setColorFilter(actualImageColorFilter);
  }
  return wrapper;
}
 
Example #14
Source File: WrappingUtils.java    From fresco with MIT License 5 votes vote down vote up
/** Wraps the parent's child with a ScaleTypeDrawable. */
static ScaleTypeDrawable wrapChildWithScaleType(
    DrawableParent parent, ScalingUtils.ScaleType scaleType) {
  Drawable child = parent.setDrawable(sEmptyDrawable);
  child = maybeWrapWithScaleType(child, scaleType);
  parent.setDrawable(child);
  Preconditions.checkNotNull(child, "Parent has no child drawable!");
  return (ScaleTypeDrawable) child;
}
 
Example #15
Source File: GenericDraweeHierarchy.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Gets the ScaleTypeDrawable at the specified index. In case there is no child at the specified
 * index, a NullPointerException is thrown. In case there is a child, but the ScaleTypeDrawable
 * does not exist, the child will be wrapped with a new ScaleTypeDrawable.
 */
private ScaleTypeDrawable getScaleTypeDrawableAtIndex(int index) {
  DrawableParent parent = getParentDrawableAtIndex(index);
  if (parent instanceof ScaleTypeDrawable) {
    return (ScaleTypeDrawable) parent;
  } else {
    return WrappingUtils.wrapChildWithScaleType(parent, ScalingUtils.ScaleType.FIT_XY);
  }
}
 
Example #16
Source File: GenericDraweeHierarchyTest.java    From fresco with MIT License 5 votes vote down vote up
private void assertScaleTypeAndDrawable(
    Drawable expectedChild, ScaleType expectedScaleType, Drawable actualBranch) {
  assertNotNull(actualBranch);
  ScaleTypeDrawable scaleTypeDrawable = (ScaleTypeDrawable) actualBranch;
  assertSame(expectedChild, scaleTypeDrawable.getCurrent());
  assertSame(expectedScaleType, scaleTypeDrawable.getScaleType());
}
 
Example #17
Source File: GenericDraweeHierarchyTest.java    From fresco with MIT License 5 votes vote down vote up
private void assertActualImageScaleType(
    ScaleType expectedScaleType, PointF expectedFocusPoint, Drawable actualBranch) {
  assertNotNull(actualBranch);
  ScaleTypeDrawable scaleTypeDrawable = (ScaleTypeDrawable) actualBranch;
  assertSame(expectedScaleType, scaleTypeDrawable.getScaleType());
  assertSame(ForwardingDrawable.class, scaleTypeDrawable.getCurrent().getClass());
  AndroidGraphicsTestUtils.assertEquals(expectedFocusPoint, scaleTypeDrawable.getFocusPoint(), 0);
}
 
Example #18
Source File: HierarcherImpl.java    From fresco with MIT License 5 votes vote down vote up
@Override
public void setupActualImageWrapper(
    ScaleTypeDrawable actualImageWrapper, ImageOptions imageOptions) {
  actualImageWrapper.setScaleType(imageOptions.getActualImageScaleType());
  actualImageWrapper.setFocusPoint(imageOptions.getActualImageFocusPoint());
  actualImageWrapper.setColorFilter(imageOptions.getActualImageColorFilter());
}
 
Example #19
Source File: UETFresco.java    From UETool with MIT License 5 votes vote down vote up
private Bitmap getFrescoDrawableBitmap(Drawable drawable) {
    try {
        if (drawable instanceof ScaleTypeDrawable) {
            return ((BitmapDrawable) drawable.getCurrent()).getBitmap();
        } else if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #20
Source File: PipelineDraweeController.java    From fresco with MIT License 5 votes vote down vote up
/**
 * updateDebugOverlay updates the debug overlay. Subclasses of {@link PipelineDraweeController}
 * can override this method (and call <code>super</code>) to provide additional debug information.
 */
protected void updateDebugOverlay(
    @Nullable CloseableImage image, DebugControllerOverlayDrawable debugOverlay) {
  debugOverlay.setControllerId(getId());

  final DraweeHierarchy draweeHierarchy = getHierarchy();
  ScaleType scaleType = null;
  if (draweeHierarchy != null) {
    final ScaleTypeDrawable scaleTypeDrawable =
        ScalingUtils.getActiveScaleTypeDrawable(draweeHierarchy.getTopLevelDrawable());
    scaleType = scaleTypeDrawable != null ? scaleTypeDrawable.getScaleType() : null;
  }
  debugOverlay.setScaleType(scaleType);

  // fill in image origin text and color hint
  final int origin = mDebugOverlayImageOriginListener.getImageOrigin();
  final String originText = ImageOriginUtils.toString(origin);
  final int originColor = DebugOverlayImageOriginColor.getImageOriginColor(origin);
  debugOverlay.setOrigin(originText, originColor);

  if (image != null) {
    debugOverlay.setDimensions(image.getWidth(), image.getHeight());
    debugOverlay.setImageSize(image.getSizeInBytes());
  } else {
    debugOverlay.reset();
  }
}
 
Example #21
Source File: GenericDraweeHierarchy.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/** Sets the actual image scale type. */
public void setActualImageScaleType(ScaleType scaleType) {
  Preconditions.checkNotNull(scaleType);
  ScaleTypeDrawable scaleTypeDrawable = findLayerScaleTypeDrawable(mActualImageIndex);
  if (scaleTypeDrawable == null) {
    throw new UnsupportedOperationException("ScaleTypeDrawable not found!");
  }
  scaleTypeDrawable.setScaleType(scaleType);
}
 
Example #22
Source File: GenericDraweeHierarchy.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/** Sets the actual image focus point. */
public void setActualImageFocusPoint(PointF focusPoint) {
  Preconditions.checkNotNull(focusPoint);
  ScaleTypeDrawable scaleTypeDrawable = findLayerScaleTypeDrawable(mActualImageIndex);
  if (scaleTypeDrawable == null) {
    throw new UnsupportedOperationException("ScaleTypeDrawable not found!");
  }
  scaleTypeDrawable.setFocusPoint(focusPoint);
}
 
Example #23
Source File: GenericDraweeHierarchy.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the ScaleTypeDrawable at the specified index, or null if not found.
 */
private @Nullable ScaleTypeDrawable findLayerScaleTypeDrawable(int index) {
  Drawable drawable = mFadeDrawable.getDrawable(index);
  if (drawable instanceof MatrixDrawable) {
    drawable = drawable.getCurrent();
  }
  if (drawable instanceof ScaleTypeDrawable) {
    return (ScaleTypeDrawable) drawable;
  } else {
    return null;
  }
}
 
Example #24
Source File: GenericDraweeHierarchy.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the parent drawable at the specified index.
 * <p>
 * If MatrixDrawable or ScaleTypeDrawable is found at that index, it will be returned as a parent.
 * Otherwise, the FadeDrawable will be returned.
 */
private Drawable findLayerParent(int index) {
  Drawable parent = mFadeDrawable;
  Drawable child = mFadeDrawable.getDrawable(index);
  if (child instanceof MatrixDrawable) {
    parent = child;
    child = parent.getCurrent();
  }
  if (child instanceof ScaleTypeDrawable) {
    parent = child;
    child = parent.getCurrent();
  }
  return parent;
}
 
Example #25
Source File: GenericDraweeHierarchy.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
private static Drawable maybeWrapWithScaleType(
    Drawable drawable,
    @Nullable ScaleType scaleType,
    @Nullable PointF focusPoint) {
  Preconditions.checkNotNull(drawable);
  if (scaleType == null) {
    return drawable;
  }
  ScaleTypeDrawable scaleTypeDrawable = new ScaleTypeDrawable(drawable, scaleType);
  if (focusPoint != null) {
    scaleTypeDrawable.setFocusPoint(focusPoint);
  }
  return scaleTypeDrawable;
}
 
Example #26
Source File: FrescoDrawable2Impl.java    From fresco with MIT License 4 votes vote down vote up
@Override
public ScaleTypeDrawable getActualImageWrapper() {
  return mActualImageWrapper;
}
 
Example #27
Source File: BaseFrescoDrawable.java    From fresco with MIT License 4 votes vote down vote up
public @Nullable PointF getActualImageFocusPoint() {
  Drawable actual = getDrawable(IMAGE_DRAWABLE_INDEX);
  if (!(actual instanceof ScaleTypeDrawable)) return null;

  return ((ScaleTypeDrawable) actual).getFocusPoint();
}
 
Example #28
Source File: BaseFrescoDrawable.java    From fresco with MIT License 4 votes vote down vote up
public @Nullable ScalingUtils.ScaleType getActualImageScaleType() {
  Drawable actual = getDrawable(IMAGE_DRAWABLE_INDEX);
  if (!(actual instanceof ScaleTypeDrawable)) return null;

  return ((ScaleTypeDrawable) actual).getScaleType();
}
 
Example #29
Source File: GenericDraweeHierarchy.java    From fresco with MIT License 4 votes vote down vote up
/** Returns whether the given layer has a scale type drawable. */
private boolean hasScaleTypeDrawableAtIndex(int index) {
  DrawableParent parent = getParentDrawableAtIndex(index);
  return (parent instanceof ScaleTypeDrawable);
}
 
Example #30
Source File: Hierarcher.java    From fresco with MIT License 2 votes vote down vote up
/**
 * Set up the actual image wrapper scale type Drawable.
 *
 * @param actualImageWrapper the wrapper to set up
 * @param imageOptions image options to be used
 */
void setupActualImageWrapper(ScaleTypeDrawable actualImageWrapper, ImageOptions imageOptions);