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 public interface PostProcessor { 41 /** 42 * Do any processing after (for example) decoding. 43 * 44 * <p>Drawing to the {@link Canvas} will behave as if the initial processing 45 * (e.g. decoding) already exists in the Canvas. An implementation can draw 46 * effects on top of this, or it can even draw behind it using 47 * {@link PorterDuff.Mode#DST_OVER PorterDuff.Mode.DST_OVER}. A common 48 * effect is to add transparency to the corners to achieve rounded corners. 49 * That can be done with the following code:</p> 50 * 51 * <pre class="prettyprint"> 52 * Path path = new Path(); 53 * path.setFillType(Path.FillType.INVERSE_EVEN_ODD); 54 * int width = canvas.getWidth(); 55 * int height = canvas.getHeight(); 56 * path.addRoundRect(0, 0, width, height, 20, 20, Path.Direction.CW); 57 * Paint paint = new Paint(); 58 * paint.setAntiAlias(true); 59 * paint.setColor(Color.TRANSPARENT); 60 * paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); 61 * canvas.drawPath(path, paint); 62 * return PixelFormat.TRANSLUCENT; 63 * </pre> 64 * 65 * 66 * @param canvas The {@link Canvas} to draw to. 67 * @return Opacity of the result after drawing. 68 * {@link PixelFormat#UNKNOWN PixelFormat.UNKNOWN} means that the 69 * implementation did not change whether the image has alpha. Return 70 * this unless you added transparency (e.g. with the code above, in 71 * which case you should return 72 * {@link PixelFormat#TRANSLUCENT PixelFormat.TRANSLUCENT}) or you 73 * forced the image to be opaque (e.g. by drawing everywhere with an 74 * opaque color and {@link PorterDuff.Mode#DST_OVER PorterDuff.Mode.DST_OVER}, 75 * in which case you should return {@link PixelFormat#OPAQUE PixelFormat.OPAQUE}). 76 * {@link PixelFormat#TRANSLUCENT PixelFormat.TRANSLUCENT} means that 77 * the implementation added transparency. This is safe to return even 78 * if the image already had transparency. This is also safe to return 79 * if the result is opaque, though it may draw more slowly. 80 * {@link PixelFormat#OPAQUE PixelFormat.OPAQUE} means that the 81 * implementation forced the image to be opaque. This is safe to return 82 * even if the image was already opaque. 83 * {@link PixelFormat#TRANSPARENT PixelFormat.TRANSPARENT} (or any other 84 * integer) is not allowed, and will result in throwing an 85 * {@link java.lang.IllegalArgumentException}. 86 */ 87 @PixelFormat.Opacity onPostProcess(@onNull Canvas canvas)88 public int onPostProcess(@NonNull Canvas canvas); 89 } 90