Class TransformationUtils


  • public final class TransformationUtils
    extends java.lang.Object
    A class with methods to efficiently resize Bitmaps.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int PAINT_FLAGS  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static android.graphics.Bitmap centerCrop​(BitmapPool pool, android.graphics.Bitmap inBitmap, int width, int height)
      A potentially expensive operation to crop the given Bitmap so that it fills the given dimensions.
      static android.graphics.Bitmap centerInside​(BitmapPool pool, android.graphics.Bitmap inBitmap, int width, int height)
      If the Bitmap is smaller or equal to the Target it returns the original size, if not then fitCenter(BitmapPool, Bitmap, int, int) is called instead.
      static android.graphics.Bitmap circleCrop​(BitmapPool pool, android.graphics.Bitmap inBitmap, int destWidth, int destHeight)
      Crop the image to a circle and resize to the specified width/height.
      static android.graphics.Bitmap fitCenter​(BitmapPool pool, android.graphics.Bitmap inBitmap, int width, int height)
      An expensive operation to resize the given Bitmap down so that it fits within the given dimensions maintain the original proportions.
      static java.util.concurrent.locks.Lock getBitmapDrawableLock()  
      static int getExifOrientationDegrees​(int exifOrientation)
      Get the # of degrees an image must be rotated to match the given exif orientation.
      static boolean isExifOrientationRequired​(int exifOrientation)
      Returns true if the given exif orientation indicates that a transformation is necessary and false otherwise.
      static android.graphics.Bitmap rotateImage​(android.graphics.Bitmap imageToOrient, int degreesToRotate)
      This is an expensive operation that copies the image in place with the pixels rotated.
      static android.graphics.Bitmap rotateImageExif​(BitmapPool pool, android.graphics.Bitmap inBitmap, int exifOrientation)
      Rotate and/or flip the image to match the given exif orientation.
      static android.graphics.Bitmap roundedCorners​(BitmapPool pool, android.graphics.Bitmap inBitmap, float topLeft, float topRight, float bottomRight, float bottomLeft)
      Creates a bitmap from a source bitmap and rounds the corners, applying a potentially different [X, Y] radius to each corner.
      static android.graphics.Bitmap roundedCorners​(BitmapPool pool, android.graphics.Bitmap inBitmap, int roundingRadius)
      Creates a bitmap from a source bitmap and rounds the corners.
      static android.graphics.Bitmap roundedCorners​(BitmapPool pool, android.graphics.Bitmap inBitmap, int width, int height, int roundingRadius)
      Deprecated.
      Width and height are unused and ignored.
      static void setAlpha​(android.graphics.Bitmap inBitmap, android.graphics.Bitmap outBitmap)
      Sets the alpha of the Bitmap we're going to re-use to the alpha of the Bitmap we're going to transform.
      • Methods inherited from class java.lang.Object

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

      • getBitmapDrawableLock

        public static java.util.concurrent.locks.Lock getBitmapDrawableLock()
      • centerCrop

        public static android.graphics.Bitmap centerCrop​(@NonNull
                                                         BitmapPool pool,
                                                         @NonNull
                                                         android.graphics.Bitmap inBitmap,
                                                         int width,
                                                         int height)
        A potentially expensive operation to crop the given Bitmap so that it fills the given dimensions. This operation is significantly less expensive in terms of memory if a mutable Bitmap with the given dimensions is passed in as well.
        Parameters:
        pool - The BitmapPool to obtain a bitmap from.
        inBitmap - The Bitmap to resize.
        width - The width in pixels of the final Bitmap.
        height - The height in pixels of the final Bitmap.
        Returns:
        The resized Bitmap (will be recycled if recycled is not null).
      • fitCenter

        public static android.graphics.Bitmap fitCenter​(@NonNull
                                                        BitmapPool pool,
                                                        @NonNull
                                                        android.graphics.Bitmap inBitmap,
                                                        int width,
                                                        int height)
        An expensive operation to resize the given Bitmap down so that it fits within the given dimensions maintain the original proportions.
        Parameters:
        pool - The BitmapPool obtain a bitmap from.
        inBitmap - The Bitmap to shrink.
        width - The width in pixels the final image will fit within.
        height - The height in pixels the final image will fit within.
        Returns:
        A new Bitmap shrunk to fit within the given dimensions, or toFit if toFit's width or height matches the given dimensions and toFit fits within the given dimensions
      • centerInside

        public static android.graphics.Bitmap centerInside​(@NonNull
                                                           BitmapPool pool,
                                                           @NonNull
                                                           android.graphics.Bitmap inBitmap,
                                                           int width,
                                                           int height)
        If the Bitmap is smaller or equal to the Target it returns the original size, if not then fitCenter(BitmapPool, Bitmap, int, int) is called instead.
        Parameters:
        pool - The BitmapPool obtain a bitmap from.
        inBitmap - The Bitmap to center.
        width - The width in pixels of the target.
        height - The height in pixels of the target.
        Returns:
        returns input Bitmap if smaller or equal to target, or toFit if the Bitmap's width or height is larger than the given dimensions
      • setAlpha

        public static void setAlpha​(android.graphics.Bitmap inBitmap,
                                    android.graphics.Bitmap outBitmap)
        Sets the alpha of the Bitmap we're going to re-use to the alpha of the Bitmap we're going to transform. This keeps Bitmap.hasAlpha()} consistent before and after the transformation for transformations that don't add or remove transparent pixels.
        Parameters:
        inBitmap - The Bitmap that will be transformed.
        outBitmap - The Bitmap that will be returned from the transformation.
      • rotateImage

        public static android.graphics.Bitmap rotateImage​(@NonNull
                                                          android.graphics.Bitmap imageToOrient,
                                                          int degreesToRotate)
        This is an expensive operation that copies the image in place with the pixels rotated. If possible rather use getOrientationMatrix, and put that as the imageMatrix on an ImageView.
        Parameters:
        imageToOrient - Image Bitmap to orient.
        degreesToRotate - number of degrees to rotate the image by. If zero the original image is returned unmodified.
        Returns:
        The oriented bitmap. May be the imageToOrient without modification, or a new Bitmap.
      • getExifOrientationDegrees

        public static int getExifOrientationDegrees​(int exifOrientation)
        Get the # of degrees an image must be rotated to match the given exif orientation.
        Parameters:
        exifOrientation - The exif orientation [1-8]
        Returns:
        the number of degrees to rotate
      • rotateImageExif

        public static android.graphics.Bitmap rotateImageExif​(@NonNull
                                                              BitmapPool pool,
                                                              @NonNull
                                                              android.graphics.Bitmap inBitmap,
                                                              int exifOrientation)
        Rotate and/or flip the image to match the given exif orientation.
        Parameters:
        pool - A pool that may or may not contain an image of the necessary dimensions.
        inBitmap - The bitmap to rotate/flip.
        exifOrientation - the exif orientation [1-8].
        Returns:
        The rotated and/or flipped image or toOrient if no rotation or flip was necessary.
      • isExifOrientationRequired

        public static boolean isExifOrientationRequired​(int exifOrientation)
        Returns true if the given exif orientation indicates that a transformation is necessary and false otherwise.
      • circleCrop

        public static android.graphics.Bitmap circleCrop​(@NonNull
                                                         BitmapPool pool,
                                                         @NonNull
                                                         android.graphics.Bitmap inBitmap,
                                                         int destWidth,
                                                         int destHeight)
        Crop the image to a circle and resize to the specified width/height. The circle crop will have the same width and height equal to the min-edge of the result image.
        Parameters:
        pool - The BitmapPool obtain a bitmap from.
        inBitmap - The Bitmap to resize.
        destWidth - The width in pixels of the final Bitmap.
        destHeight - The height in pixels of the final Bitmap.
        Returns:
        The resized Bitmap (will be recycled if recycled is not null).
      • roundedCorners

        @Deprecated
        public static android.graphics.Bitmap roundedCorners​(@NonNull
                                                             BitmapPool pool,
                                                             @NonNull
                                                             android.graphics.Bitmap inBitmap,
                                                             int width,
                                                             int height,
                                                             int roundingRadius)
        Deprecated.
        Width and height are unused and ignored. Use roundedCorners(BitmapPool, Bitmap, int) instead.
        Creates a bitmap from a source bitmap and rounds the corners.
        Parameters:
        inBitmap - the source bitmap to use as a basis for the created bitmap.
        width - the width of the generated bitmap.
        height - the height of the generated bitmap.
        roundingRadius - the corner radius to be applied (in device-specific pixels).
        Returns:
        a Bitmap similar to inBitmap but with rounded corners.
        Throws:
        java.lang.IllegalArgumentException - if roundingRadius, width or height is 0 or less.
      • roundedCorners

        public static android.graphics.Bitmap roundedCorners​(@NonNull
                                                             BitmapPool pool,
                                                             @NonNull
                                                             android.graphics.Bitmap inBitmap,
                                                             int roundingRadius)
        Creates a bitmap from a source bitmap and rounds the corners.

        This method does NOT resize the given Bitmap, it only rounds it's corners. To both resize and round the corners of an image, consider BaseRequestOptions.transform(Transformation[]) and/or MultiTransformation.

        Parameters:
        inBitmap - the source bitmap to use as a basis for the created bitmap.
        roundingRadius - the corner radius to be applied (in device-specific pixels).
        Returns:
        a Bitmap similar to inBitmap but with rounded corners.
        Throws:
        java.lang.IllegalArgumentException - if roundingRadius, width or height is 0 or less.
      • roundedCorners

        public static android.graphics.Bitmap roundedCorners​(@NonNull
                                                             BitmapPool pool,
                                                             @NonNull
                                                             android.graphics.Bitmap inBitmap,
                                                             float topLeft,
                                                             float topRight,
                                                             float bottomRight,
                                                             float bottomLeft)
        Creates a bitmap from a source bitmap and rounds the corners, applying a potentially different [X, Y] radius to each corner.

        This method does NOT resize the given Bitmap, it only rounds it's corners. To both resize and round the corners of an image, consider BaseRequestOptions.transform(Transformation[]) and/or MultiTransformation.

        Parameters:
        inBitmap - the source bitmap to use as a basis for the created bitmap.
        topLeft - top-left radius
        topRight - top-right radius
        bottomRight - bottom-right radius
        bottomLeft - bottom-left radius
        Returns:
        a Bitmap similar to inBitmap but with rounded corners.