Package com.android.ide.common.util
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 classAssetUtil.Effectstatic classAssetUtil.FillEffectA fill, defined by a paint.static classAssetUtil.ShadowEffectAn 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.BufferedImageblurredImage(java.awt.image.BufferedImage source, double radius)Applies a gaussian blur of the given radius to the givenBufferedImageusing a kernel convolution.static voiddrawCenterCrop(java.awt.Graphics2D g, java.awt.image.BufferedImage source, java.awt.Rectangle dstRect)Draws the givenBufferedImageto the canvas, centered and cropped to fill the bounds defined by the destination rectangle, and with preserved aspect ratio.static voiddrawCentered(java.awt.Graphics2D g, java.awt.image.BufferedImage source, java.awt.Rectangle imageRect)Draws the givenBufferedImageto the canvas, centered, wholly contained within the bounds defined by the destination rectangle, and with preserved aspect ratio.static voiddrawCenterInside(java.awt.Graphics2D g, java.awt.image.BufferedImage source, java.awt.Rectangle dstRect)Draws the givenBufferedImageto the canvas, centered, wholly contained within the bounds defined by the destination rectangle, and with preserved aspect ratio.static voiddrawCenterScaled(java.awt.Graphics2D g, java.awt.image.BufferedImage source, java.awt.Rectangle dstRect, int scaleRectWidth, int scaleRectHeight)Draws the givenBufferedImagesource, centered and with preserved aspect ratio, to the given destination rectangledestRectinside the canvasg, so that either the width or height of the image matches the corresponding width or height of the given reference rectanglescaleRect.static voiddrawEffects(java.awt.Graphics2D g, java.awt.image.BufferedImage source, int x, int y, AssetUtil.Effect[] effects)Draws the givenBufferedImageto the canvas, at the given coordinates, with the givenAssetUtil.Effects applied.static java.awt.image.BufferedImagefilledImage(java.awt.image.BufferedImage source, java.awt.Paint paint)Fills the givenBufferedImagewith aPaint, preserving its alpha channel.static java.awt.image.BufferedImageinvertedAlphaImage(java.awt.image.BufferedImage source)Inverts the alpha channel of the givenBufferedImage.static java.awt.image.BufferedImagenewArgbBufferedImage(int width, int height)Creates a new ARGBBufferedImageof the given width and height.static java.awt.image.BufferedImageoperatedImage(java.awt.image.BufferedImage source, java.awt.image.BufferedImageOp op)Applies aBufferedImageOpon the givenBufferedImage.static java.awt.image.BufferedImagepaddedImage(java.awt.image.BufferedImage source, int padding)Pads the givenBufferedImageon all sides by the given padding amount.static java.awt.image.BufferedImagescaledImage(java.awt.image.BufferedImage source, int width, int height)Smoothly scales the givenBufferedImageto the given width and height using theImage.SCALE_SMOOTHalgorithm (generally bicubic resampling or bilinear filtering).static java.awt.image.BufferedImagetrimmedImage(java.awt.image.BufferedImage source)Trims the transparent pixels from the givenBufferedImage(returns a sub-image).
-
Field Details
-
Constructor Details
-
AssetUtil
public AssetUtil()
-
-
Method Details
-
newArgbBufferedImage
public static java.awt.image.BufferedImage newArgbBufferedImage(int width, int height)Creates a new ARGBBufferedImageof 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 givenBufferedImageto the given width and height using theImage.SCALE_SMOOTHalgorithm (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 givenBufferedImageusing 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 givenBufferedImage. 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 aBufferedImageOpon the givenBufferedImage.- 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 givenBufferedImagewith aPaint, 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 givenBufferedImageon 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 givenBufferedImage(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 givenBufferedImageto the canvas, at the given coordinates, with the givenAssetUtil.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 givenBufferedImageto 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 givenBufferedImageto 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 givenBufferedImagesource, centered and with preserved aspect ratio, to the given destination rectangledestRectinside the canvasg, so that either the width or height of the image matches the corresponding width or height of the given reference rectanglescaleRect.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 givenBufferedImageto 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.
-