Class AssetUtil

java.lang.Object
com.android.ide.common.util.AssetUtil

public class AssetUtil
extends java.lang.Object
A set of utility classes for manipulating BufferedImage objects and drawing them to Graphics2D canvases.
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    static class  AssetUtil.Effect
    static class  AssetUtil.FillEffect
    A fill, defined by a paint.
    static class  AssetUtil.ShadowEffect
    An inner or outer shadow.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static AssetUtil.Effect[] NO_EFFECTS  
  • Constructor Summary

    Constructors 
    Constructor Description
    AssetUtil()  
  • Method Summary

    Modifier and Type Method Description
    static java.awt.image.BufferedImage blurredImage​(java.awt.image.BufferedImage source, double radius)
    Applies a gaussian blur of the given radius to the given BufferedImage using a kernel convolution.
    static void drawCenterCrop​(java.awt.Graphics2D g, java.awt.image.BufferedImage source, java.awt.Rectangle dstRect)
    Draws the given BufferedImage to the canvas, centered and cropped to fill the bounds defined by the destination rectangle, and with preserved aspect ratio.
    static void drawCentered​(java.awt.Graphics2D g, java.awt.image.BufferedImage source, java.awt.Rectangle imageRect)
    Draws the given BufferedImage to the canvas, centered, wholly contained within the bounds defined by the destination rectangle, and with preserved aspect ratio.
    static void drawCenterInside​(java.awt.Graphics2D g, java.awt.image.BufferedImage source, java.awt.Rectangle dstRect)
    Draws the given BufferedImage to the canvas, centered, wholly contained within the bounds defined by the destination rectangle, and with preserved aspect ratio.
    static void drawCenterScaled​(java.awt.Graphics2D g, java.awt.image.BufferedImage source, java.awt.Rectangle dstRect, int scaleRectWidth, int scaleRectHeight)
    Draws the given BufferedImage source, centered and with preserved aspect ratio, to the given destination rectangle destRect inside the canvas g , so that either the width or height of the image matches the corresponding width or height of the given reference rectangle scaleRect.
    static void drawEffects​(java.awt.Graphics2D g, java.awt.image.BufferedImage source, int x, int y, AssetUtil.Effect[] effects)
    Draws the given BufferedImage to the canvas, at the given coordinates, with the given AssetUtil.Effects applied.
    static java.awt.image.BufferedImage filledImage​(java.awt.image.BufferedImage source, java.awt.Paint paint)
    Fills the given BufferedImage with a Paint, preserving its alpha channel.
    static java.awt.image.BufferedImage invertedAlphaImage​(java.awt.image.BufferedImage source)
    Inverts the alpha channel of the given BufferedImage.
    static java.awt.image.BufferedImage newArgbBufferedImage​(int width, int height)
    Creates a new ARGB BufferedImage of the given width and height.
    static java.awt.image.BufferedImage operatedImage​(java.awt.image.BufferedImage source, java.awt.image.BufferedImageOp op)
    Applies a BufferedImageOp on the given BufferedImage.
    static java.awt.image.BufferedImage paddedImage​(java.awt.image.BufferedImage source, int padding)
    Pads the given BufferedImage on all sides by the given padding amount.
    static java.awt.image.BufferedImage scaledImage​(java.awt.image.BufferedImage source, int width, int height)
    Smoothly scales the given BufferedImage to the given width and height using the Image.SCALE_SMOOTH algorithm (generally bicubic resampling or bilinear filtering).
    static java.awt.image.BufferedImage trimmedImage​(java.awt.image.BufferedImage source)
    Trims the transparent pixels from the given BufferedImage (returns a sub-image).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

  • Method Details

    • newArgbBufferedImage

      public static java.awt.image.BufferedImage newArgbBufferedImage​(int width, int height)
      Creates a new ARGB BufferedImage of the given width and height.
      Parameters:
      width - The width of the new image.
      height - The height of the new image.
      Returns:
      The newly created image.
    • scaledImage

      public static java.awt.image.BufferedImage scaledImage​(java.awt.image.BufferedImage source, int width, int height)
      Smoothly scales the given BufferedImage to the given width and height using the Image.SCALE_SMOOTH algorithm (generally bicubic resampling or bilinear filtering).
      Parameters:
      source - The source image.
      width - The destination width to scale to.
      height - The destination height to scale to.
      Returns:
      A new, scaled image (or the original image if no scaling is needed).
    • blurredImage

      public static java.awt.image.BufferedImage blurredImage​(java.awt.image.BufferedImage source, double radius)
      Applies a gaussian blur of the given radius to the given BufferedImage using a kernel convolution.
      Parameters:
      source - The source image.
      radius - The blur radius, in pixels.
      Returns:
      A new, blurred image, or the source image if no blur is performed.
    • invertedAlphaImage

      public static java.awt.image.BufferedImage invertedAlphaImage​(java.awt.image.BufferedImage source)
      Inverts the alpha channel of the given BufferedImage. RGB data for the inverted area are undefined, so it's generally best to fill the resulting image with a color.
      Parameters:
      source - The source image.
      Returns:
      A new image with an alpha channel inverted from the original.
    • operatedImage

      public static java.awt.image.BufferedImage operatedImage​(java.awt.image.BufferedImage source, java.awt.image.BufferedImageOp op)
      Applies a BufferedImageOp on the given BufferedImage.
      Parameters:
      source - The source image.
      op - The operation to perform.
      Returns:
      A new image with the operation performed.
    • filledImage

      public static java.awt.image.BufferedImage filledImage​(java.awt.image.BufferedImage source, java.awt.Paint paint)
      Fills the given BufferedImage with a Paint, preserving its alpha channel.
      Parameters:
      source - The source image.
      paint - The paint to fill with.
      Returns:
      A new, painted/filled image.
    • paddedImage

      public static java.awt.image.BufferedImage paddedImage​(java.awt.image.BufferedImage source, int padding)
      Pads the given BufferedImage on all sides by the given padding amount.
      Parameters:
      source - The source image.
      padding - The amount to pad on all sides, in pixels.
      Returns:
      A new, padded image, or the source image if no padding is performed.
    • trimmedImage

      public static java.awt.image.BufferedImage trimmedImage​(java.awt.image.BufferedImage source)
      Trims the transparent pixels from the given BufferedImage (returns a sub-image).
      Parameters:
      source - The source image.
      Returns:
      A new, trimmed image, or the source image if no trim is performed.
    • drawEffects

      public static void drawEffects​(java.awt.Graphics2D g, java.awt.image.BufferedImage source, int x, int y, AssetUtil.Effect[] effects)
      Draws the given BufferedImage to the canvas, at the given coordinates, with the given AssetUtil.Effects applied. Note that drawn effects may be outside the bounds of the source image.
      Parameters:
      g - The destination canvas.
      source - The source image.
      x - The x offset at which to draw the image.
      y - The y offset at which to draw the image.
      effects - The list of effects to apply.
    • drawCenterInside

      public static void drawCenterInside​(java.awt.Graphics2D g, java.awt.image.BufferedImage source, java.awt.Rectangle dstRect)
      Draws the given BufferedImage to the canvas, centered, wholly contained within the bounds defined by the destination rectangle, and with preserved aspect ratio.
      Parameters:
      g - The destination canvas.
      source - The source image.
      dstRect - The destination rectangle in the destination canvas into which to draw the image.
    • drawCenterCrop

      public static void drawCenterCrop​(java.awt.Graphics2D g, java.awt.image.BufferedImage source, java.awt.Rectangle dstRect)
      Draws the given BufferedImage to the canvas, centered and cropped to fill the bounds defined by the destination rectangle, and with preserved aspect ratio.
      Parameters:
      g - The destination canvas.
      source - The source image.
      dstRect - The destination rectangle in the destination canvas into which to draw the image.
    • drawCenterScaled

      public static void drawCenterScaled​(java.awt.Graphics2D g, java.awt.image.BufferedImage source, java.awt.Rectangle dstRect, int scaleRectWidth, int scaleRectHeight)
      Draws the given BufferedImage source, centered and with preserved aspect ratio, to the given destination rectangle destRect inside the canvas g , so that either the width or height of the image matches the corresponding width or height of the given reference rectangle scaleRect.

      This method is similar to drawCenterCrop(Graphics2D, BufferedImage, Rectangle) in term of determining the scaling factor, but this method does not crop the resulting scaled image.

      Parameters:
      g - The destination canvas.
      source - The source image.
      dstRect - The destination rectangle in the destination canvas into which to draw the image.
      scaleRectWidth - The width of the rectangle used a reference to scale the source image.
      scaleRectHeight - The height of the rectangle used a reference to scale the source image.
    • drawCentered

      public static void drawCentered​(java.awt.Graphics2D g, java.awt.image.BufferedImage source, java.awt.Rectangle imageRect)
      Draws the given BufferedImage to the canvas, centered, wholly contained within the bounds defined by the destination rectangle, and with preserved aspect ratio.
      Parameters:
      g - The destination canvas.
      source - The source image.
      imageRect - The destination rectangle in the destination canvas into which to draw the image.