Class BitmapTransformation
- java.lang.Object
-
- com.bumptech.glide.load.resource.bitmap.BitmapTransformation
-
- All Implemented Interfaces:
Key,Transformation<android.graphics.Bitmap>
- Direct Known Subclasses:
CenterCrop,CenterInside,CircleCrop,FitCenter,GranularRoundedCorners,Rotate,RoundedCorners
public abstract class BitmapTransformation extends java.lang.Object implements Transformation<android.graphics.Bitmap>
A simpleTransformationfor transformingBitmaps that abstracts away dealing withResourceobjects for subclasses.Use cases will look something like this:
public class FillSpace extends BitmapTransformation { private static final String ID = "com.bumptech.glide.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(notClass.getName()to avoid proguard obfuscation) is an easy way to implementKey.updateDiskCacheKey(java.security.MessageDigest)} correctly. If additional arguments are required they can be passed in to the constructor of theTransformationand then used to update theMessageDigestpassed in toKey.updateDiskCacheKey(MessageDigest). If arguments are primitive types, they can typically easily be serialized usingByteBuffer.Stringtypes can be serialized withString.getBytes(Charset)using the constantKey.CHARSET.As with all
Transformations, all subclasses must implementObject.equals(Object)andObject.hashCode()for memory caching to work correctly.
-
-
Field Summary
-
Fields inherited from interface com.bumptech.glide.load.Key
CHARSET, STRING_CHARSET_NAME
-
-
Constructor Summary
Constructors Constructor Description BitmapTransformation()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Resource<android.graphics.Bitmap>transform(android.content.Context context, Resource<android.graphics.Bitmap> resource, int outWidth, int outHeight)Transforms the given resource and returns the transformed resource.protected abstract android.graphics.Bitmaptransform(BitmapPool pool, android.graphics.Bitmap toTransform, int outWidth, int outHeight)Transforms the givenBitmapbased on the given dimensions and returns the transformed result.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.bumptech.glide.load.Key
equals, hashCode, updateDiskCacheKey
-
-
-
-
Method Detail
-
transform
@NonNull public final Resource<android.graphics.Bitmap> transform(@NonNull android.content.Context context, @NonNull Resource<android.graphics.Bitmap> resource, int outWidth, int outHeight)
Description copied from interface:TransformationTransforms 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(), andKey.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.NONEor make sureKey.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:
transformin interfaceTransformation<android.graphics.Bitmap>- Parameters:
context- The Application contextresource- The resource to transform.outWidth- The width of the view or target the resource will be displayed in, orTarget.SIZE_ORIGINALto indicate the original resource width.outHeight- The height of the view or target the resource will be displayed in, orTarget.SIZE_ORIGINALto indicate the original resource height.- Returns:
- The transformed resource.
-
transform
protected abstract android.graphics.Bitmap transform(@NonNull BitmapPool pool, @NonNull android.graphics.Bitmap toTransform, int outWidth, int outHeight)Transforms the givenBitmapbased on the given dimensions and returns the transformed result.The provided Bitmap, toTransform, should not be recycled or returned to the pool. Glide will automatically recycle and/or reuse toTransform if the transformation returns a different Bitmap. Similarly implementations should never recycle or return Bitmaps that are returned as the result of this method. Recycling or returning the provided and/or the returned Bitmap to the pool will lead to a variety of runtime exceptions and drawing errors. See #408 for an example. If the implementation obtains and discards intermediate Bitmaps, they may safely be returned to the BitmapPool and/or recycled.
outWidth and outHeight will never be
Target.SIZE_ORIGINAL, this class converts them to be the size of the Bitmap we're going to transform before calling this method.- Parameters:
pool- ABitmapPoolthat can be used to obtain and return intermediateBitmaps used in this transformation. For everyBitmapobtained from the pool during this transformation, aBitmapmust also be returned.toTransform- TheBitmapto transform.outWidth- The ideal width of the transformed bitmap (the transformed width does not need to match exactly).outHeight- The ideal height of the transformed bitmap (the transformed height does not need to match exactly).
-
-