com.android.ide.common.util
Class AssetUtil

java.lang.Object
  extended by 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
static class AssetUtil.Effect
          An effect to apply in drawEffects(java.awt.Graphics2D, java.awt.image.BufferedImage, int, int, AssetUtil.Effect[])
static class AssetUtil.FillEffect
          A fill, defined by a paint.
static class AssetUtil.ShadowEffect
          An inner or outer shadow.
 
Constructor Summary
AssetUtil()
           
 
Method Summary
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 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 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.Rectangle scaleRectangle(java.awt.Rectangle rect, float scaleFactor)
          Scales the given rectangle by the given scale factor.
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
 

Constructor Detail

AssetUtil

public AssetUtil()
Method Detail

scaleRectangle

public static java.awt.Rectangle scaleRectangle(java.awt.Rectangle rect,
                                                float scaleFactor)
Scales the given rectangle by the given scale factor.

Parameters:
rect - The rectangle to scale.
scaleFactor - The factor to scale by.
Returns:
The scaled rectangle.

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.

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.