Package 

Class BitmapTransformation

  • All Implemented Interfaces:
    com.mobwith.imgmodule.load.Key , com.mobwith.imgmodule.load.Transformation

    
    public abstract class BitmapTransformation
     implements Transformation<Bitmap>
                        

    A simple com.mobwith.imgmodule.load.Transformation for transforming s that abstracts away dealing with objects for subclasses.

    Use cases will look something like this:

     public class FillSpace extends BitmapTransformation { private static final String ID = "com.mobwith.imgmodule.ImageModule.transformations.FillSpace"; private static final byte[] ID_BYTES = ID.getBytes(Charset.forName("UTF-8")); {@literal @Override} public Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { if (toTransform.getWidth() == outWidth && toTransform.getHeight() == outHeight) { return toTransform; } return Bitmap.createScaledBitmap(toTransform, outWidth, outHeight, true); } {@literal @Override} public boolean equals(Object o) { return o instanceof FillSpace; } {@literal @Override} public int hashCode() { return ID.hashCode(); } {@literal @Override} public void updateDiskCacheKey(MessageDigest messageDigest) { messageDigest.update(ID_BYTES); } } 
    

    Using the fully qualified class name as a static final String (not to avoid proguard obfuscation) is an easy way to implement } correctly. If additional arguments are required they can be passed in to the constructor of the {@code Transformation} and then used to update the MessageDigest passed in to . If arguments are primitive types, they can typically easily be serialized using java.nio.ByteBuffer. String types can be serialized with getBytes using the constant CHARSET.

    As with all Transformations, all subclasses must implement and hashCode for memory caching to work correctly.

    • Method Summary

      Modifier and Type Method Description
      final Resource<Bitmap> transform(@NonNull() Context context, @NonNull() Resource<Bitmap> resource, int outWidth, int outHeight) Transforms the given resource and returns the transformed resource.
      • Methods inherited from class com.mobwith.imgmodule.load.Transformation

        transform
      • Methods inherited from class com.mobwith.imgmodule.load.Key

        equals, hashCode, updateDiskCacheKey
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • transform

        @NonNull() final Resource<Bitmap> transform(@NonNull() Context context, @NonNull() Resource<Bitmap> resource, int outWidth, int outHeight)

        Transforms the given resource and returns the transformed resource.

        If the original resource object is not returned, the original resource will be recycled andit's internal resources may be reused. This means it is not safe to rely on the originalresource 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 objectcan be returned with modified internal state, or the data in the original resource can becopied into the transformed resource.

        If a Transformation is updated, equals, hashCode, and should all change. If you're using a simpleString key an easy way to do this is to append a version number to your key. Failing to do sowill mean users may see images loaded from cache that had the old version of the Transformationapplied. Changing the return values of those methods will ensure that the cache key has changedand therefore that any cached resources will be re-generated using the updated Transformation.

        During development you may need to either using or make sure changes each time you make a change to theTransformation. Otherwise the resource you request may be loaded from disk cache and yourTransformation may not be called.

        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 to indicate the original resourcewidth.
        outHeight - The height of the view or target the resource will be displayed in, or to indicate the original resourceheight.