Stay organized with collections
Save and categorize content based on your preferences.
PostProcessor
public
interface
PostProcessor
android.graphics.PostProcessor
|
Helper interface for adding custom processing to an image.
The image being processed may be a Drawable
, a Bitmap
, or
a frame of an AnimatedImageDrawable
produced by ImageDecoder
.
This is called before the requested object is returned.
This custom processing can even be applied to images that will be returned
as immutable objects, such as a Bitmap
with Config
Bitmap.Config.HARDWARE
returned by ImageDecoder
.
On an AnimatedImageDrawable
, the callback will only be called once,
but the drawing commands will be applied to each frame, as if the Canvas
had been returned by Picture.beginRecording
.
Supplied to ImageDecoder via setPostProcessor
.
Summary
Public methods |
abstract
int
|
onPostProcess(Canvas canvas)
Do any processing after (for example) decoding.
|
Public methods
onPostProcess
public abstract int onPostProcess (Canvas canvas)
Do any processing after (for example) decoding.
Drawing to the Canvas
will behave as if the initial processing
(e.g. decoding) already exists in the Canvas. An implementation can draw
effects on top of this, or it can even draw behind it using
PorterDuff.Mode.DST_OVER
. A common
effect is to add transparency to the corners to achieve rounded corners.
That can be done with the following code:
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;
Parameters |
canvas |
Canvas : The Canvas to draw to.
This value cannot be null . |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[null,null,["Last updated 2025-02-10 UTC."],[],[],null,["# PostProcessor\n\nAdded in [API level 28](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\nPostProcessor\n=============\n\n*** ** * ** ***\n\n[Kotlin](/reference/kotlin/android/graphics/PostProcessor \"View this page in Kotlin\") \\|Java\n\n\n`\npublic\n\n\ninterface\nPostProcessor\n`\n\n\n`\n\n\n`\n\n|--------------------------------|\n| android.graphics.PostProcessor |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nHelper interface for adding custom processing to an image.\n\nThe image being processed may be a [Drawable](/reference/android/graphics/drawable/Drawable), a [Bitmap](/reference/android/graphics/Bitmap), or\na frame of an [AnimatedImageDrawable](/reference/android/graphics/drawable/AnimatedImageDrawable) produced by [ImageDecoder](/reference/android/graphics/ImageDecoder).\nThis is called before the requested object is returned.\n\nThis custom processing can even be applied to images that will be returned\nas immutable objects, such as a [Bitmap](/reference/android/graphics/Bitmap) with `Config`\n[Bitmap.Config.HARDWARE](/reference/android/graphics/Bitmap.Config#HARDWARE) returned by [ImageDecoder](/reference/android/graphics/ImageDecoder).\n\nOn an [AnimatedImageDrawable](/reference/android/graphics/drawable/AnimatedImageDrawable), the callback will only be called once,\nbut the drawing commands will be applied to each frame, as if the [Canvas](/reference/android/graphics/Canvas)\nhad been returned by [Picture.beginRecording](/reference/android/graphics/Picture#beginRecording(int,%20int)).\n\n\nSupplied to ImageDecoder via [setPostProcessor](/reference/android/graphics/ImageDecoder#setPostProcessor(android.graphics.PostProcessor)).\n\n\u003cbr /\u003e\n\nSummary\n-------\n\n| ### Public methods ||\n|-----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` abstract int` | ` `[onPostProcess](/reference/android/graphics/PostProcessor#onPostProcess(android.graphics.Canvas))`(`[Canvas](/reference/android/graphics/Canvas)` canvas) ` Do any processing after (for example) decoding. |\n\nPublic methods\n--------------\n\n### onPostProcess\n\nAdded in [API level 28](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract int onPostProcess (Canvas canvas)\n```\n\nDo any processing after (for example) decoding.\n\nDrawing to the [Canvas](/reference/android/graphics/Canvas) will behave as if the initial processing\n(e.g. decoding) already exists in the Canvas. An implementation can draw\neffects on top of this, or it can even draw behind it using\n[PorterDuff.Mode.DST_OVER](/reference/android/graphics/PorterDuff.Mode#DST_OVER). A common\neffect is to add transparency to the corners to achieve rounded corners.\nThat can be done with the following code: \n\n```scdoc\n Path path = new Path();\n path.setFillType(Path.FillType.INVERSE_EVEN_ODD);\n int width = canvas.getWidth();\n int height = canvas.getHeight();\n path.addRoundRect(0, 0, width, height, 20, 20, Path.Direction.CW);\n Paint paint = new Paint();\n paint.setAntiAlias(true);\n paint.setColor(Color.TRANSPARENT);\n paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));\n canvas.drawPath(path, paint);\n return PixelFormat.TRANSLUCENT;\n \n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Parameters ||\n|----------|------------------------------------------------------------------------------------------------------------|\n| `canvas` | `Canvas`: The [Canvas](/reference/android/graphics/Canvas) to draw to. This value cannot be `null`. \u003cbr /\u003e |\n\n| Returns ||\n|-------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `int` | Opacity of the result after drawing. [PixelFormat.UNKNOWN](/reference/android/graphics/PixelFormat#UNKNOWN) means that the implementation did not change whether the image has alpha. Return this unless you added transparency (e.g. with the code above, in which case you should return [PixelFormat.TRANSLUCENT](/reference/android/graphics/PixelFormat#TRANSLUCENT)) or you forced the image to be opaque (e.g. by drawing everywhere with an opaque color and [PorterDuff.Mode.DST_OVER](/reference/android/graphics/PorterDuff.Mode#DST_OVER), in which case you should return [PixelFormat.OPAQUE](/reference/android/graphics/PixelFormat#OPAQUE)). [PixelFormat.TRANSLUCENT](/reference/android/graphics/PixelFormat#TRANSLUCENT) means that the implementation added transparency. This is safe to return even if the image already had transparency. This is also safe to return if the result is opaque, though it may draw more slowly. [PixelFormat.OPAQUE](/reference/android/graphics/PixelFormat#OPAQUE) means that the implementation forced the image to be opaque. This is safe to return even if the image was already opaque. [PixelFormat.TRANSPARENT](/reference/android/graphics/PixelFormat#TRANSPARENT) (or any other integer) is not allowed, and will result in throwing an [IllegalArgumentException](/reference/java/lang/IllegalArgumentException). Value is [PixelFormat.UNKNOWN](/reference/android/graphics/PixelFormat#UNKNOWN), [PixelFormat.TRANSLUCENT](/reference/android/graphics/PixelFormat#TRANSLUCENT), [PixelFormat.TRANSPARENT](/reference/android/graphics/PixelFormat#TRANSPARENT), or [PixelFormat.OPAQUE](/reference/android/graphics/PixelFormat#OPAQUE) \u003cbr /\u003e |"]]