ImageDecoder
class ImageDecoder : AutoCloseable
| kotlin.Any | |
| ↳ | android.graphics.ImageDecoder |
A class for converting encoded images (like PNG, JPEG, WEBP, GIF, or HEIF) into Drawable or Bitmap objects.
To use it, first create a Source using one of the createSource overloads. For example, to decode from a Uri, call createSource(android.content.ContentResolver,android.net.Uri) and pass the result to decodeDrawable(android.graphics.ImageDecoder.Source) or decodeBitmap(android.graphics.ImageDecoder.Source):
File file = new File(...); ImageDecoder.Source source = ImageDecoder.createSource(file); Drawable drawable = ImageDecoder.decodeDrawable(source);
To change the default settings, pass the Source and an OnHeaderDecodedListener to decodeDrawable(android.graphics.ImageDecoder.Source,android.graphics.ImageDecoder.OnHeaderDecodedListener) or decodeBitmap(android.graphics.ImageDecoder.Source,android.graphics.ImageDecoder.OnHeaderDecodedListener). For example, to create a sampled image with half the width and height of the original image, call setTargetSampleSize(2) inside onHeaderDecoded:
OnHeaderDecodedListener listener = new OnHeaderDecodedListener() { public void onHeaderDecoded(ImageDecoder decoder, ImageInfo info, Source source) { decoder.setTargetSampleSize(2); } }; Drawable drawable = ImageDecoder.decodeDrawable(source, listener);
The ImageInfo contains information about the encoded image, like its width and height, and the Source can be used to match to a particular Source if a single OnHeaderDecodedListener is used with multiple Source objects.
The OnHeaderDecodedListener can also be implemented as a lambda:
Drawable drawable = ImageDecoder.decodeDrawable(source, (decoder, info, src) -> { decoder.setTargetSampleSize(2); });
If the encoded image is an animated GIF or WEBP, #decodeDrawable will return an AnimatedImageDrawable. To start its animation, call AnimatedImageDrawable.start():
Drawable drawable = ImageDecoder.decodeDrawable(source); if (drawable instanceof AnimatedImageDrawable) { ((AnimatedImageDrawable) drawable).start(); }
By default, a Bitmap created by ImageDecoder (including one that is inside a Drawable) will be immutable (i.e. Bitmap.isMutable() returns false), and it will typically have Config Bitmap.Config.HARDWARE. Although these properties can be changed with setMutableRequired(true) (which is only compatible with decodeBitmap(android.graphics.ImageDecoder.Source) and decodeBitmap(android.graphics.ImageDecoder.Source,android.graphics.ImageDecoder.OnHeaderDecodedListener)) and setAllocator, it is also possible to apply custom effects regardless of the mutability of the final returned object by passing a PostProcessor to setPostProcessor. A PostProcessor can also be a lambda:
Drawable drawable = ImageDecoder.decodeDrawable(source, (decoder, info, src) -> { decoder.setPostProcessor((canvas) -> { // This will create rounded corners. Path path = new Path(); path.setFillType(Path.FillType.INVERSE_EVEN_ODD); int width = canvas.getWidth(); int height = canvas.getHeight(); path.addRoundRect(0, 0, width, height, 20, 20, Path.Direction.CW); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.TRANSPARENT); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); canvas.drawPath(path, paint); return PixelFormat.TRANSLUCENT; }); });
If the encoded image is incomplete or contains an error, or if an Exception occurs during decoding, a DecodeException will be thrown. In some cases, the ImageDecoder may have decoded part of the image. In order to display the partial image, an OnPartialImageListener must be passed to setOnPartialImageListener. For example:
Drawable drawable = ImageDecoder.decodeDrawable(source, (decoder, info, src) -> { decoder.setOnPartialImageListener((DecodeException e) -> { // Returning true indicates to create a Drawable or Bitmap even // if the whole image could not be decoded. Any remaining lines // will be blank. return true; }); });
Summary
| Nested classes | |
|---|---|
|
Information about an interrupted decode. |
|
|
Information about an encoded image. |
|
| abstract |
Interface for changing the default settings of a decode. |
| abstract |
Interface for inspecting a |
| abstract |
Source of encoded image data. |
| Constants | |
|---|---|
| static Int |
Use the default allocation for the pixel memory. |
| static Int |
Require a |
| static Int |
Use shared memory for the pixel memory. |
| static Int |
Use a software allocation for the pixel memory. |
| static Int |
Use the most natural |
| static Int |
Save memory if possible by using a denser |
| Public methods | |
|---|---|
| Unit |
close()Closes this resource, relinquishing any underlying resources. |
| static ImageDecoder.Source |
createSource(cr: ContentResolver, uri: Uri)Create a new |
| static ImageDecoder.Source |
createSource(assets: AssetManager, fileName: String)Create a new |
| static ImageDecoder.Source |
createSource(res: Resources, resId: Int)Create a new |
| static ImageDecoder.Source |
createSource(data: ByteArray)Create a new |
| static ImageDecoder.Source |
createSource(data: ByteArray, offset: Int, length: Int)Create a new |
| static ImageDecoder.Source |
createSource(file: File)Create a new |
| static ImageDecoder.Source |
createSource(buffer: ByteBuffer)Create a new |
| static ImageDecoder.Source |
createSource(callable: Callable<AssetFileDescriptor!>)Create a new |
| static Bitmap |
Create a |
| static Bitmap |
decodeBitmap(src: ImageDecoder.Source, listener: ImageDecoder.OnHeaderDecodedListener)Create a |
| static Drawable |
Create a |
| static Drawable |
decodeDrawable(src: ImageDecoder.Source, listener: ImageDecoder.OnHeaderDecodedListener)Create a |
| Int |
Return the allocator for the pixel memory. |
| Rect? |
getCrop()Return the cropping rectangle, if set. |
| Int |
Retrieve the memory policy for the decoded |
| ImageDecoder.OnPartialImageListener? |
Return the |
| PostProcessor? |
Return the |
| Boolean |
Return whether to treat single channel input as alpha. |
| static Boolean |
isMimeTypeSupported(mimeType: String)Return if the given MIME type is a supported file format that can be decoded by this class. |
| Boolean |
Return whether the decoded |
| Boolean |
Return whether the |
| Unit |
setAllocator(allocator: Int)Choose the backing for the pixel memory. |
| Unit |
Crop the output to |
| Unit |
setDecodeAsAlphaMaskEnabled(enabled: Boolean)Specify whether to potentially treat the output as an alpha mask. |
| Unit |
setMemorySizePolicy(policy: Int)Specify the memory policy for the decoded |
| Unit |
setMutableRequired(mutable: Boolean)Specify whether the |
| Unit |
Set (replace) the |
| Unit |
setPostProcessor(postProcessor: PostProcessor?)Modify the image after decoding and scaling. |
| Unit |
setTargetColorSpace(colorSpace: ColorSpace!)Specify the desired |
| Unit |
setTargetSampleSize(sampleSize: Int)Set the target size with a sampleSize. |
| Unit |
setTargetSize(width: Int, height: Int) |
| Unit |
setUnpremultipliedRequired(unpremultipliedRequired: Boolean)Specify whether the |
| Protected methods | |
|---|---|
| Unit |
finalize() |
Constants
ALLOCATOR_DEFAULT
static val ALLOCATOR_DEFAULT: Int
Use the default allocation for the pixel memory. Will typically result in a Bitmap.Config.HARDWARE allocation, but may be software for small images. In addition, this will switch to software when HARDWARE is incompatible, e.g. setMutableRequired(true) or setDecodeAsAlphaMaskEnabled(true).
Value: 0ALLOCATOR_HARDWARE
static val ALLOCATOR_HARDWARE: Int
Require a Bitmap.Config.HARDWARE Bitmap.
When this is combined with incompatible options, like setMutableRequired(true) or setDecodeAsAlphaMaskEnabled(true), #decodeDrawable or #decodeBitmap will throw an java.lang.IllegalStateException.
Value: 3ALLOCATOR_SHARED_MEMORY
static val ALLOCATOR_SHARED_MEMORY: Int
Use shared memory for the pixel memory.
Useful for sharing across processes.
Value: 2ALLOCATOR_SOFTWARE
static val ALLOCATOR_SOFTWARE: Int
Use a software allocation for the pixel memory.
Useful for drawing to a software Canvas or for accessing the pixels on the final output.
Value: 1MEMORY_POLICY_DEFAULT
static val MEMORY_POLICY_DEFAULT: Int
Use the most natural Bitmap.Config for the internal Bitmap.
This is the recommended default for most applications and usages. This will use the closest Bitmap.Config for the encoded source. If the encoded source does not exactly match any Bitmap.Config, the next highest quality Bitmap.Config will be used avoiding any loss in image quality.
Value: 1MEMORY_POLICY_LOW_RAM
static val MEMORY_POLICY_LOW_RAM: Int
Save memory if possible by using a denser Bitmap.Config at the cost of some image quality.
For example an opaque 8-bit image may be compressed into an Bitmap.Config.RGB_565 configuration, sacrificing image quality to save memory.
Value: 0Public methods
close
fun close(): Unit
Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try-with-resources statement.
This is an implementation detail of ImageDecoder, and should never be called manually.
| Exceptions | |
|---|---|
java.lang.Exception |
if this resource cannot be closed |
createSource
static fun createSource(
cr: ContentResolver,
uri: Uri
): ImageDecoder.Source
Create a new Source from a android.net.Uri.
Accepts the following URI schemes:
- content (
ContentResolver.SCHEME_CONTENT) - android.resource (
ContentResolver.SCHEME_ANDROID_RESOURCE) - file (
ContentResolver.SCHEME_FILE)
This method is safe to call from any thread.
| Parameters | |
|---|---|
cr |
ContentResolver: to retrieve from. This value cannot be null. |
uri |
Uri: of the image file. This value cannot be null. |
| Return | |
|---|---|
ImageDecoder.Source |
a new Source object, which can be passed to #decodeDrawable or #decodeBitmap. This value cannot be null. |
createSource
static fun createSource(
assets: AssetManager,
fileName: String
): ImageDecoder.Source
Create a new Source from a file in the "assets" directory.
This method is safe to call from any thread.
| Parameters | |
|---|---|
assets |
AssetManager: This value cannot be null. |
fileName |
String: This value cannot be null. |
| Return | |
|---|---|
ImageDecoder.Source |
This value cannot be null. |
createSource
static fun createSource(
res: Resources,
resId: Int
): ImageDecoder.Source
Create a new Source from a resource.
This method is safe to call from any thread.
| Parameters | |
|---|---|
res |
Resources: the Resources object containing the image data. This value cannot be null. |
resId |
Int: resource ID of the image data. |
| Return | |
|---|---|
ImageDecoder.Source |
a new Source object, which can be passed to #decodeDrawable or #decodeBitmap. This value cannot be null. |
createSource
static fun createSource(data: ByteArray): ImageDecoder.Source
Create a new Source from a byte array.
Note: If this Source is passed to #decodeDrawable, and the encoded image is animated, the returned AnimatedImageDrawable will continue reading from data, so its contents must not be modified, even after the AnimatedImageDrawable is returned. data's contents should never be modified during decode.
This method is safe to call from any thread.
| Parameters | |
|---|---|
data |
ByteArray: byte array of compressed image data. This value cannot be null. |
| Return | |
|---|---|
ImageDecoder.Source |
a new Source object, which can be passed to #decodeDrawable or #decodeBitmap. This value cannot be null. |
| Exceptions | |
|---|---|
java.lang.NullPointerException |
if data is null. |
createSource
static fun createSource(
data: ByteArray,
offset: Int,
length: Int
): ImageDecoder.Source
Create a new Source from a byte array.
Note: If this Source is passed to #decodeDrawable, and the encoded image is animated, the returned AnimatedImageDrawable will continue reading from data, so its contents must not be modified, even after the AnimatedImageDrawable is returned. data's contents should never be modified during decode.
This method is safe to call from any thread.
| Parameters | |
|---|---|
data |
ByteArray: byte array of compressed image data. This value cannot be null. |
offset |
Int: offset into data for where the decoder should begin parsing. |
length |
Int: number of bytes, beginning at offset, to parse. |
| Return | |
|---|---|
ImageDecoder.Source |
a new Source object, which can be passed to #decodeDrawable or #decodeBitmap. This value cannot be null. |
| Exceptions | |
|---|---|
java.lang.NullPointerException |
if data is null. |
java.lang.ArrayIndexOutOfBoundsException |
if offset and length are not within data. |
createSource
static fun createSource(file: File): ImageDecoder.Source
Create a new Source from a java.io.File.
This method should only be used for files that you have direct access to; if you'd like to work with files hosted outside your app, use an API like createSource(java.util.concurrent.Callable) or createSource(android.content.ContentResolver,android.net.Uri).
This method is safe to call from any thread.
| Parameters | |
|---|---|
file |
File: This value cannot be null. |
| Return | |
|---|---|
ImageDecoder.Source |
a new Source object, which can be passed to #decodeDrawable or #decodeBitmap. This value cannot be null. |
createSource
static fun createSource(buffer: ByteBuffer): ImageDecoder.Source
Create a new Source from a java.nio.ByteBuffer.
Decoding will start from buffer.position(). The position of buffer will not be affected.
Note: If this Source is passed to #decodeDrawable, and the encoded image is animated, the returned AnimatedImageDrawable will continue reading from the buffer, so its contents must not be modified, even after the AnimatedImageDrawable is returned. buffer's contents should never be modified during decode.
This method is safe to call from any thread.
| Parameters | |
|---|---|
buffer |
ByteBuffer: This value cannot be null. |
| Return | |
|---|---|
ImageDecoder.Source |
a new Source object, which can be passed to #decodeDrawable or #decodeBitmap. This value cannot be null. |
createSource
static fun createSource(callable: Callable<AssetFileDescriptor!>): ImageDecoder.Source
Create a new Source from a Callable that returns a new AssetFileDescriptor for each request. This provides control over how the AssetFileDescriptor is created, such as passing options into android.content.ContentResolver#openTypedAssetFileDescriptor, or enabling use of a android.os.CancellationSignal.
It's important for the given Callable to return a new, unique AssetFileDescriptor for each invocation, to support reuse of the returned Source.
This method is safe to call from any thread.
| Parameters | |
|---|---|
callable |
Callable<AssetFileDescriptor!>: This value cannot be null. |
| Return | |
|---|---|
ImageDecoder.Source |
a new Source object, which can be passed to #decodeDrawable or #decodeBitmap. This value cannot be null. |
decodeBitmap
static fun decodeBitmap(src: ImageDecoder.Source): Bitmap
Create a Bitmap from a Source.
Since there is no OnHeaderDecodedListener, the default settings will be used. In order to change any settings, call decodeBitmap(android.graphics.ImageDecoder.Source,android.graphics.ImageDecoder.OnHeaderDecodedListener) instead.
This method may take several seconds to complete, so it should only be called from a worker thread.
| Parameters | |
|---|---|
src |
ImageDecoder.Source: representing the encoded image. This value cannot be null. |
| Return | |
|---|---|
Bitmap |
Bitmap containing the image. This value cannot be null. |
| Exceptions | |
|---|---|
java.io.IOException |
if src is not found, is an unsupported format, or cannot be decoded for any reason. |
decodeBitmap
static fun decodeBitmap(
src: ImageDecoder.Source,
listener: ImageDecoder.OnHeaderDecodedListener
): Bitmap
Create a Bitmap from a Source.
This method may take several seconds to complete, so it should only be called from a worker thread.
| Parameters | |
|---|---|
src |
ImageDecoder.Source: representing the encoded image. This value cannot be null. |
listener |
ImageDecoder.OnHeaderDecodedListener: for learning the ImageInfo and changing any default settings on the ImageDecoder. This will be called on the same thread as decodeBitmap before that method returns. This is required in order to change any of the default settings. This value cannot be null. |
| Return | |
|---|---|
Bitmap |
Bitmap containing the image. This value cannot be null. |
| Exceptions | |
|---|---|
java.io.IOException |
if src is not found, is an unsupported format, or cannot be decoded for any reason. |
decodeDrawable
static fun decodeDrawable(src: ImageDecoder.Source): Drawable
Create a Drawable from a Source.
Since there is no OnHeaderDecodedListener, the default settings will be used. In order to change any settings, call decodeDrawable(android.graphics.ImageDecoder.Source,android.graphics.ImageDecoder.OnHeaderDecodedListener) instead.
This method may take several seconds to complete, so it should only be called from a worker thread.
| Parameters | |
|---|---|
src |
ImageDecoder.Source: representing the encoded image. This value cannot be null. |
| Return | |
|---|---|
Drawable |
Drawable for displaying the image. This value cannot be null. |
| Exceptions | |
|---|---|
java.io.IOException |
if src is not found, is an unsupported format, or cannot be decoded for any reason. |
decodeDrawable
static fun decodeDrawable(
src: ImageDecoder.Source,
listener: ImageDecoder.OnHeaderDecodedListener
): Drawable
Create a Drawable from a Source.
This method may take several seconds to complete, so it should only be called from a worker thread.
| Parameters | |
|---|---|
src |
ImageDecoder.Source: representing the encoded image. This value cannot be null. |
listener |
ImageDecoder.OnHeaderDecodedListener: for learning the ImageInfo and changing any default settings on the ImageDecoder. This will be called on the same thread as decodeDrawable before that method returns. This is required in order to change any of the default settings. This value cannot be null. |
| Return | |
|---|---|
Drawable |
Drawable for displaying the image. This value cannot be null. |
| Exceptions | |
|---|---|
java.io.IOException |
if src is not found, is an unsupported format, or cannot be decoded for any reason. |
getAllocator
fun getAllocator(): Int
Return the allocator for the pixel memory.
getCrop
fun getCrop(): Rect?
Return the cropping rectangle, if set.
| Return | |
|---|---|
Rect? |
This value may be null. |
getMemorySizePolicy
fun getMemorySizePolicy(): Int
Retrieve the memory policy for the decoded Bitmap.
| Return | |
|---|---|
Int |
Value is android.graphics.ImageDecoder#MEMORY_POLICY_DEFAULT, or android.graphics.ImageDecoder#MEMORY_POLICY_LOW_RAM |
getOnPartialImageListener
fun getOnPartialImageListener(): ImageDecoder.OnPartialImageListener?
Return the OnPartialImageListener currently set.
| Return | |
|---|---|
ImageDecoder.OnPartialImageListener? |
This value may be null. |
getPostProcessor
fun getPostProcessor(): PostProcessor?
Return the PostProcessor currently set.
| Return | |
|---|---|
PostProcessor? |
This value may be null. |
isDecodeAsAlphaMaskEnabled
fun isDecodeAsAlphaMaskEnabled(): Boolean
Return whether to treat single channel input as alpha.
This returns whether setDecodeAsAlphaMaskEnabled was set to true. It may still return true even if the image has more than one channel and therefore will not be treated as an alpha mask.
isMimeTypeSupported
static fun isMimeTypeSupported(mimeType: String): Boolean
Return if the given MIME type is a supported file format that can be decoded by this class. This can be useful to determine if a file can be decoded directly, or if it needs to be converted into a more general format using an API like ContentResolver.openTypedAssetFile.
| Parameters | |
|---|---|
mimeType |
String: This value cannot be null. |
isMutableRequired
fun isMutableRequired(): Boolean
Return whether the decoded Bitmap will be mutable.
isUnpremultipliedRequired
fun isUnpremultipliedRequired(): Boolean
Return whether the Bitmap will have unpremultiplied pixels.
setAllocator
fun setAllocator(allocator: Int): Unit
Choose the backing for the pixel memory.
This is ignored for animated drawables.
Like all setters on ImageDecoder, this must be called inside onHeaderDecoded.
| Parameters | |
|---|---|
allocator |
Int: Type of allocator to use. Value is android.graphics.ImageDecoder#ALLOCATOR_DEFAULT, android.graphics.ImageDecoder#ALLOCATOR_SOFTWARE, android.graphics.ImageDecoder#ALLOCATOR_SHARED_MEMORY, or android.graphics.ImageDecoder#ALLOCATOR_HARDWARE |
setCrop
fun setCrop(subset: Rect?): Unit
Crop the output to subset of the (possibly) scaled image.
subset must be contained within the size set by setTargetSize or the bounds of the image if setTargetSize was not called. Otherwise an IllegalStateException will be thrown by #decodeDrawable/#decodeBitmap.
NOT intended as a replacement for BitmapRegionDecoder.decodeRegion(). This supports all formats, but merely crops the output.
Like all setters on ImageDecoder, this must be called inside onHeaderDecoded.
| Parameters | |
|---|---|
subset |
Rect?: This value may be null. |
setDecodeAsAlphaMaskEnabled
fun setDecodeAsAlphaMaskEnabled(enabled: Boolean): Unit
Specify whether to potentially treat the output as an alpha mask.
If this is set to true and the image is encoded in a format with only one channel, treat that channel as alpha. Otherwise this call has no effect.
This is incompatible with ALLOCATOR_HARDWARE. Trying to combine them will result in #decodeDrawable/ #decodeBitmap throwing an java.lang.IllegalStateException.
Like all setters on ImageDecoder, this must be called inside onHeaderDecoded.
setMemorySizePolicy
fun setMemorySizePolicy(policy: Int): Unit
Specify the memory policy for the decoded Bitmap.
Like all setters on ImageDecoder, this must be called inside onHeaderDecoded.
| Parameters | |
|---|---|
policy |
Int: Value is android.graphics.ImageDecoder#MEMORY_POLICY_DEFAULT, or android.graphics.ImageDecoder#MEMORY_POLICY_LOW_RAM |
setMutableRequired
fun setMutableRequired(mutable: Boolean): Unit
Specify whether the Bitmap should be mutable.
By default, a Bitmap created by #decodeBitmap will be immutable i.e. Bitmap.isMutable() returns false. This can be changed with setMutableRequired(true).
Mutable Bitmaps are incompatible with ALLOCATOR_HARDWARE, because Bitmap.Config.HARDWARE Bitmaps cannot be mutable. Attempting to combine them will throw an java.lang.IllegalStateException.
Mutable Bitmaps are also incompatible with #decodeDrawable, which would require retrieving the Bitmap from the returned Drawable in order to modify. Attempting to decode a mutable Drawable will throw an java.lang.IllegalStateException.
Like all setters on ImageDecoder, this must be called inside onHeaderDecoded.
setOnPartialImageListener
fun setOnPartialImageListener(listener: ImageDecoder.OnPartialImageListener?): Unit
Set (replace) the OnPartialImageListener on this object.
Will be called if there is an error in the input. Without one, an error will result in an Exception being thrown.
Like all setters on ImageDecoder, this must be called inside onHeaderDecoded.
| Parameters | |
|---|---|
listener |
ImageDecoder.OnPartialImageListener?: This value may be null. |
setPostProcessor
fun setPostProcessor(postProcessor: PostProcessor?): Unit
Modify the image after decoding and scaling.
This allows adding effects prior to returning a Drawable or Bitmap. For a Drawable or an immutable Bitmap, this is the only way to process the image after decoding.
If combined with setTargetSize and/or setCrop, PostProcessor.onPostProcess occurs last.
If set on a nine-patch image, the nine-patch data is ignored.
For an animated image, the drawing commands drawn on the Canvas will be recorded immediately and then applied to each frame.
Like all setters on ImageDecoder, this must be called inside onHeaderDecoded.
| Parameters | |
|---|---|
postProcessor |
PostProcessor?: This value may be null. |
setTargetColorSpace
fun setTargetColorSpace(colorSpace: ColorSpace!): Unit
Specify the desired ColorSpace for the output.
If non-null, the decoder will try to decode into colorSpace. If it is null, which is the default, or the request cannot be met, the decoder will pick either the color space embedded in the image or the ColorSpace best suited for the requested image configuration (for instance sRGB for the Bitmap.Config.ARGB_8888 configuration and EXTENDED_SRGB for Bitmap.Config.RGBA_F16).
Only ColorSpace.Model.RGB color spaces are currently supported. An IllegalArgumentException will be thrown by #decodeDrawable/ #decodeBitmap when setting a non-RGB color space such as Lab.
Prior to android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE, the specified color space's transfer function must be an ICC parametric curve. An IllegalArgumentException will be thrown by the decode methods if calling ColorSpace.Rgb.getTransferParameters() on the specified color space returns null. Starting from android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE, the color spaces with non ICC parametric curve transfer function are allowed. E.g., BT2020_HLG.
Like all setters on ImageDecoder, this must be called inside onHeaderDecoded.
setTargetSampleSize
fun setTargetSampleSize(sampleSize: Int): Unit
Set the target size with a sampleSize.
By default, the output size will match the size of the encoded image, which can be retrieved from the ImageInfo in onHeaderDecoded.
Requests the decoder to subsample the original image, returning a smaller image to save memory. The sampleSize is the number of pixels in either dimension that correspond to a single pixel in the output. For example, sampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels.
Must be greater than or equal to 1.
This has the same effect as calling setTargetSize with dimensions based on the sampleSize. Unlike dividing the original width and height by the sampleSize manually, calling this method allows ImageDecoder to round in the direction that it can do most efficiently.
Only the last call to this or setTargetSize is respected.
Like all setters on ImageDecoder, this must be called inside onHeaderDecoded.
| Parameters | |
|---|---|
sampleSize |
Int: sampling rate of the encoded image. Value is 1 or greater |
setTargetSize
fun setTargetSize(
width: Int,
height: Int
): Unit
Specify the size of the output Drawable or Bitmap.
By default, the output size will match the size of the encoded image, which can be retrieved from the ImageInfo in onHeaderDecoded.
This will sample or scale the output to an arbitrary size that may be smaller or larger than the encoded size.
Only the last call to this or setTargetSampleSize is respected.
Like all setters on ImageDecoder, this must be called inside onHeaderDecoded.
| Parameters | |
|---|---|
width |
Int: width in pixels of the output, must be greater than 0 The units of this value are pixels. Value is 1 or greater |
height |
Int: height in pixels of the output, must be greater than 0 The units of this value are pixels. Value is 1 or greater |
setUnpremultipliedRequired
fun setUnpremultipliedRequired(unpremultipliedRequired: Boolean): Unit
Specify whether the Bitmap should have unpremultiplied pixels.
By default, ImageDecoder will create a Bitmap with premultiplied pixels, which is required for drawing with the android.view.View system (i.e. to a Canvas). Calling this method with a value of true will result in #decodeBitmap returning a Bitmap with unpremultiplied pixels. See Bitmap.isPremultiplied(). This is incompatible with #decodeDrawable; attempting to decode an unpremultiplied Drawable will throw an java.lang.IllegalStateException.
Like all setters on ImageDecoder, this must be called inside onHeaderDecoded.
Protected methods
finalize
protected fun finalize(): Unit
| Exceptions | |
|---|---|
java.lang.Throwable |
the Exception raised by this method |