Class Image
- All Implemented Interfaces:
AutoCloseable
Image
describes a two-dimensional array of pixels to sample from. The pixels
may be located in CPU memory, in GPU memory as a GPU texture, or be lazily-generated.
Image
cannot be modified by CPU or GPU after it is created.Image
width and height are greater than zero.Image
may be created fromBitmap
, Surface, Picture and GPU texture.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Indicates that the image was created for an unknown pixel density and will not be scaled. -
Method Summary
Modifier and TypeMethodDescriptionfinal Image
clone()
Create a shallow copy of this image, this is equivalent to creating a shared owner for the image.void
close()
Perform a deferred cleanup if the underlying resource is not released.static Image
Creates a new image object representing the target resource image.static Image
createTextureFromBitmap
(RecordingContext recordingContext, Bitmap bitmap) Create an image that backed by a GPU texture with the given bitmap.static Image
createTextureFromBitmap
(Bitmap bitmap) Create an image that backed by a GPU texture with the given bitmap.void
Copy the image's bounds to the specified Rect.void
Copy the image's bounds to the specified RectF.Returns the color space associated with this image.int
Returns the density for this image.int
Returns the intrinsic height of this image.getInfo()
Returns theImageInfo
describing the width, height, color type, alpha type and color space of this image.int
getScaledHeight
(int targetDensity) Convenience method that returns the height of this image divided by the density scale factor.int
getScaledWidth
(int targetDensity) Convenience method that returns the width of this image divided by the density scale factor.int
getWidth()
Returns the intrinsic width of this image.boolean
Returns whether this Image has only an alpha channel (i.e.boolean
isClosed()
Returns true if this image has been closed.boolean
isOpaque()
Returns whether the Image is completely opaque.void
setDensity
(int density) Specifies the density for this image.toString()
-
Field Details
-
DENSITY_NONE
public static final int DENSITY_NONEIndicates that the image was created for an unknown pixel density and will not be scaled.- See Also:
-
-
Method Details
-
createTextureFromBitmap
Create an image that backed by a GPU texture with the given bitmap. Whether the bitmap is immutable or not, the bitmap can be safely closed after the call.Must be called after render system and UI system are initialized successfully, must be called from UI thread.
This method may fail if:
- Bitmap is null or closed
- GPU device is lost (disconnected)
- The width or height of the bitmap exceeds the maximum dimension supported by the GPU
- The format of bitmap is not directly or indirectly supported by the GPU
- Unable to allocate sufficient GPU-only memory for the GPU texture
- Unable to allocate sufficient host memory for the staging buffer
- Parameters:
bitmap
- the source bitmap- Returns:
- image, or null if failed
- Throws:
NullPointerException
- no GPU contextIllegalStateException
- not call from UI thread
-
createTextureFromBitmap
@Experimental @Nullable public static Image createTextureFromBitmap(@NonNull RecordingContext recordingContext, @NonNull Bitmap bitmap) Create an image that backed by a GPU texture with the given bitmap. Whether the bitmap is immutable or not, the bitmap can be safely closed after the call.- Parameters:
recordingContext
- the recording graphics context on the current threadbitmap
- the source bitmap- Returns:
- image, or null if failed
- Throws:
NullPointerException
- bitmap is null or releasedIllegalStateException
- not call from context thread, or no context
-
create
Creates a new image object representing the target resource image. You should use a single image as the UI texture to avoid each icon creating its own image. Underlying resources are automatically released.Do NOT close the returned Image.
- Parameters:
namespace
- the application namespaceentry
- the sub path to the resource- Returns:
- the image
-
getInfo
Returns theImageInfo
describing the width, height, color type, alpha type and color space of this image.- Returns:
- image info
-
getWidth
public int getWidth()Returns the intrinsic width of this image.- Returns:
- image width in pixels
-
getHeight
public int getHeight()Returns the intrinsic height of this image.- Returns:
- image height in pixels
-
getScaledWidth
public int getScaledWidth(int targetDensity) Convenience method that returns the width of this image divided by the density scale factor.Returns the image's width multiplied by the ratio of the target density to the image's source density
- Parameters:
targetDensity
- The density of the target canvas of the image.- Returns:
- The scaled width of this image, according to the density scale factor.
-
getScaledHeight
public int getScaledHeight(int targetDensity) Convenience method that returns the height of this image divided by the density scale factor.Returns the image's height multiplied by the ratio of the target density to the image's source density
- Parameters:
targetDensity
- The density of the target canvas of the image.- Returns:
- The scaled height of this image, according to the density scale factor.
-
getDensity
public int getDensity()Returns the density for this image.
The default density is
DisplayMetrics.DENSITY_DEFAULT
..- Returns:
- A scaling factor of the default density or
DENSITY_NONE
if the scaling factor is unknown. - See Also:
-
setDensity
public void setDensity(int density) Specifies the density for this image. When the image is drawn to a
Canvas
or with aImageDrawable
that also has a density, it will be scaled appropriately.- Parameters:
density
- The density scaling factor to use with this image orDENSITY_NONE
if the density is unknown.- See Also:
-
getBounds
Copy the image's bounds to the specified Rect.- Parameters:
bounds
- Rect to receive the image's bounds
-
getBounds
Copy the image's bounds to the specified RectF.- Parameters:
bounds
- RectF to receive the image's bounds
-
isAlphaMask
public boolean isAlphaMask()Returns whether this Image has only an alpha channel (i.e. a transparency mask). If so, then when drawn its color depends on the solid color of Paint, or Paint's shader if set.- Returns:
- whether the image is alpha only
-
isOpaque
public boolean isOpaque()Returns whether the Image is completely opaque. Returns true if this Image has no alpha channel, or is flagged to be known that all of its pixels are opaque.- Returns:
- whether the image is opaque
-
getColorSpace
Returns the color space associated with this image. If the color space is unknown, this method returns null. -
close
public void close()Perform a deferred cleanup if the underlying resource is not released. Manually mark the underlying resources closed, if needed. After this, you will not be able to operate this object, and its GPU resource will be reclaimed as soon as possible after use.If this Image object was not created by you (for example, from Resources), then you must not call this method.
When this object becomes phantom-reachable, the system will automatically do this cleanup operation.
- Specified by:
close
in interfaceAutoCloseable
-
isClosed
public boolean isClosed()Returns true if this image has been closed. If so, then it is an error to try to access it.- Returns:
- true if the image has been closed
-
toString
-
clone
Create a shallow copy of this image, this is equivalent to creating a shared owner for the image. You may change the density or close the returned Image without affecting the original Image.- Overrides:
clone
in classObject
- Returns:
- a shallow copy of image
- Throws:
IllegalStateException
- this image is already closed
-