1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.graphics; 18 19 import android.annotation.NonNull; 20 import android.graphics.drawable.AnimatedImageDrawable; 21 import android.graphics.drawable.Drawable; 22 23 /** 24 * Helper interface for adding custom processing to an image. 25 * 26 * <p>The image being processed may be a {@link Drawable}, a {@link Bitmap}, or 27 * a frame of an {@link AnimatedImageDrawable} produced by {@link ImageDecoder}. 28 * This is called before the requested object is returned.</p> 29 * 30 * <p>This custom processing can even be applied to images that will be returned 31 * as immutable objects, such as a {@link Bitmap} with {@code Config} 32 * {@link Bitmap.Config#HARDWARE} returned by {@link ImageDecoder}.</p> 33 * 34 * <p>On an {@link AnimatedImageDrawable}, the callback will only be called once, 35 * but the drawing commands will be applied to each frame, as if the {@link Canvas} 36 * had been returned by {@link Picture#beginRecording Picture.beginRecording}.<p> 37 * 38 * <p>Supplied to ImageDecoder via {@link ImageDecoder#setPostProcessor setPostProcessor}.</p> 39 */ 40 @android.ravenwood.annotation.RavenwoodKeepWholeClass 41 public interface PostProcessor { 42 /** 43 * Do any processing after (for example) decoding. 44 * 45 * <p>Drawing to the {@link Canvas} will behave as if the initial processing 46 * (e.g. decoding) already exists in the Canvas. An implementation can draw 47 * effects on top of this, or it can even draw behind it using 48 * {@link PorterDuff.Mode#DST_OVER PorterDuff.Mode.DST_OVER}. A common 49 * effect is to add transparency to the corners to achieve rounded corners. 50 * That can be done with the following code:</p> 51 * 52 * <pre class="prettyprint"> 53 * Path path = new Path(); 54 * path.setFillType(Path.FillType.INVERSE_EVEN_ODD); 55 * int width = canvas.getWidth(); 56 * int height = canvas.getHeight(); 57 * path.addRoundRect(0, 0, width, height, 20, 20, Path.Direction.CW); 58 * Paint paint = new Paint(); 59 * paint.setAntiAlias(true); 60 * paint.setColor(Color.TRANSPARENT); 61 * paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); 62 * canvas.drawPath(path, paint); 63 * return PixelFormat.TRANSLUCENT; 64 * </pre> 65 * 66 * 67 * @param canvas The {@link Canvas} to draw to. 68 * @return Opacity of the result after drawing. 69 * {@link PixelFormat#UNKNOWN PixelFormat.UNKNOWN} means that the 70 * implementation did not change whether the image has alpha. Return 71 * this unless you added transparency (e.g. with the code above, in 72 * which case you should return 73 * {@link PixelFormat#TRANSLUCENT PixelFormat.TRANSLUCENT}) or you 74 * forced the image to be opaque (e.g. by drawing everywhere with an 75 * opaque color and {@link PorterDuff.Mode#DST_OVER PorterDuff.Mode.DST_OVER}, 76 * in which case you should return {@link PixelFormat#OPAQUE PixelFormat.OPAQUE}). 77 * {@link PixelFormat#TRANSLUCENT PixelFormat.TRANSLUCENT} means that 78 * the implementation added transparency. This is safe to return even 79 * if the image already had transparency. This is also safe to return 80 * if the result is opaque, though it may draw more slowly. 81 * {@link PixelFormat#OPAQUE PixelFormat.OPAQUE} means that the 82 * implementation forced the image to be opaque. This is safe to return 83 * even if the image was already opaque. 84 * {@link PixelFormat#TRANSPARENT PixelFormat.TRANSPARENT} (or any other 85 * integer) is not allowed, and will result in throwing an 86 * {@link java.lang.IllegalArgumentException}. 87 */ 88 @PixelFormat.Opacity onPostProcess(@onNull Canvas canvas)89 public int onPostProcess(@NonNull Canvas canvas); 90 } 91