All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bumptech.glide.load.resource.bitmap.BitmapTransformation Maven / Gradle / Ivy

Go to download

A fast and efficient image loading library for Android focused on smooth scrolling.

There is a newer version: 5.0.0-rc01
Show newest version
package com.bumptech.glide.load.resource.bitmap;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.util.Util;

/**
 * A simple {@link com.bumptech.glide.load.Transformation} for transforming
 * {@link android.graphics.Bitmap}s that abstracts away dealing with
 * {@link com.bumptech.glide.load.engine.Resource} objects for subclasses.
 *
 * Use cases will look something like this:
 * 
 * 
 * public class FillSpace extends BaseBitmapTransformation {
 *     private static final String ID = "com.bumptech.glide.transformations.FillSpace";
 *     private static final String ID_BYTES = ID.getBytes(STRING_CHARSET_NAME);
 *
 *     {@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 void equals(Object o) {
 *       return o instanceof FillSpace;
 *     }
 *
 *     {@literal @Override}
 *     public int hashCode() {
 *       return ID.hashCode();
 *     }
 *
 *     {@literal @Override}
 *     public void updateDiskCacheKey(MessageDigest messageDigest)
 *         throws UnsupportedEncodingException {
 *       messageDigest.update(ID_BYTES);
 *     }
 * }
 * 
 * 
*/ public abstract class BitmapTransformation implements Transformation { public BitmapTransformation() { // Intentionally empty. } @Deprecated public BitmapTransformation(@SuppressWarnings("unused") Context context) { this(); } @Deprecated public BitmapTransformation(@SuppressWarnings("unused") BitmapPool bitmapPool) { this(); } @Override public final Resource transform( Context context, Resource resource, int outWidth, int outHeight) { if (!Util.isValidDimensions(outWidth, outHeight)) { throw new IllegalArgumentException( "Cannot apply transformation on width: " + outWidth + " or height: " + outHeight + " less than or equal to zero and not Target.SIZE_ORIGINAL"); } BitmapPool bitmapPool = Glide.get(context).getBitmapPool(); Bitmap toTransform = resource.get(); int targetWidth = outWidth == Target.SIZE_ORIGINAL ? toTransform.getWidth() : outWidth; int targetHeight = outHeight == Target.SIZE_ORIGINAL ? toTransform.getHeight() : outHeight; Bitmap transformed = transform(bitmapPool, toTransform, targetWidth, targetHeight); final Resource result; if (toTransform.equals(transformed)) { result = resource; } else { result = BitmapResource.obtain(transformed, bitmapPool); } return result; } /** * Transforms the given {@link android.graphics.Bitmap} based 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 * {@link com.bumptech.glide.request.target.Target#SIZE_ORIGINAL}, * this class converts them to be the size of the Bitmap we're going to transform before calling * this method. * * @param pool A {@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool} that can * be used to obtain and return intermediate {@link Bitmap}s used in this * transformation. For every {@link android.graphics.Bitmap} obtained from the * pool during this transformation, a {@link android.graphics.Bitmap} must also * be returned. * @param toTransform The {@link android.graphics.Bitmap} to transform. * @param outWidth The ideal width of the transformed bitmap (the transformed width does not * need to match exactly). * @param outHeight The ideal height of the transformed bitmap (the transformed height does not * need to match exactly). */ protected abstract Bitmap transform( @NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight); }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy