Class DrawableTransformation

  • All Implemented Interfaces:
    Key, Transformation<android.graphics.drawable.Drawable>

    public class DrawableTransformation
    extends java.lang.Object
    implements Transformation<android.graphics.drawable.Drawable>
    Applies a Bitmap Transformation to Drawables by first attempting to convert the Drawable to a Bitmap and then running the Transformation on the converted Bitmap.

    This class is relatively efficient for BitmapDrawable where the Bitmap is readily accessible. For non-Bitmap based Drawables, this class must first try to draw the Drawable to a Bitmap using Canvas, which is less efficient. Drawables that implement Animatable will fail with an exception. Drawables that return <= 0 for Drawable.getIntrinsicHeight() and/or Drawable.getIntrinsicWidth() will fail with an exception if the requested size is Target.SIZE_ORIGINAL. Drawables without intrinsic dimensions are drawn using the dimensions provided in Transformation.transform(Context, Resource, int, int). As a result, they may be transformed incorrectly or in unexpected ways.

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Transformation<android.graphics.drawable.BitmapDrawable> asBitmapDrawable()  
      boolean equals​(java.lang.Object o)
      For caching to work correctly, implementations must implement this method and Key.hashCode().
      int hashCode()
      For caching to work correctly, implementations must implement this method and Key.equals(Object).
      Resource<android.graphics.drawable.Drawable> transform​(android.content.Context context, Resource<android.graphics.drawable.Drawable> resource, int outWidth, int outHeight)
      Transforms the given resource and returns the transformed resource.
      void updateDiskCacheKey​(java.security.MessageDigest messageDigest)
      Adds all uniquely identifying information to the given digest.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DrawableTransformation

        public DrawableTransformation​(Transformation<android.graphics.Bitmap> wrapped,
                                      boolean isRequired)
    • Method Detail

      • asBitmapDrawable

        public Transformation<android.graphics.drawable.BitmapDrawable> asBitmapDrawable()
      • transform

        @NonNull
        public Resource<android.graphics.drawable.Drawable> transform​(@NonNull
                                                                      android.content.Context context,
                                                                      @NonNull
                                                                      Resource<android.graphics.drawable.Drawable> resource,
                                                                      int outWidth,
                                                                      int outHeight)
        Description copied from interface: Transformation
        Transforms the given resource and returns the transformed resource.

        If the original resource object is not returned, the original resource will be recycled and it's internal resources may be reused. This means it is not safe to rely on the original resource or any internal state of the original resource in any new resource that is created. Usually this shouldn't occur, but if absolutely necessary either the original resource object can be returned with modified internal state, or the data in the original resource can be copied into the transformed resource.

        If a Transformation is updated, Key.equals(Object), Key.hashCode(), and Key.updateDiskCacheKey(java.security.MessageDigest) should all change. If you're using a simple String key an easy way to do this is to append a version number to your key. Failing to do so will mean users may see images loaded from cache that had the old version of the Transformation applied. Changing the return values of those methods will ensure that the cache key has changed and therefore that any cached resources will be re-generated using the updated Transformation.

        During development you may need to either using DiskCacheStrategy.NONE or make sure Key.updateDiskCacheKey(java.security.MessageDigest) changes each time you make a change to the Transformation. Otherwise the resource you request may be loaded from disk cache and your Transformation may not be called.

        Specified by:
        transform in interface Transformation<android.graphics.drawable.Drawable>
        Parameters:
        context - The Application context
        resource - The resource to transform.
        outWidth - The width of the view or target the resource will be displayed in, or Target.SIZE_ORIGINAL to indicate the original resource width.
        outHeight - The height of the view or target the resource will be displayed in, or Target.SIZE_ORIGINAL to indicate the original resource height.
        Returns:
        The transformed resource.
      • equals

        public boolean equals​(java.lang.Object o)
        Description copied from interface: Key
        For caching to work correctly, implementations must implement this method and Key.hashCode().
        Specified by:
        equals in interface Key
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Description copied from interface: Key
        For caching to work correctly, implementations must implement this method and Key.equals(Object).
        Specified by:
        hashCode in interface Key
        Overrides:
        hashCode in class java.lang.Object
      • updateDiskCacheKey

        public void updateDiskCacheKey​(@NonNull
                                       java.security.MessageDigest messageDigest)
        Description copied from interface: Key
        Adds all uniquely identifying information to the given digest.

        Note - Using MessageDigest.reset() inside of this method will result in undefined behavior.

        Specified by:
        updateDiskCacheKey in interface Key