summaryrefslogtreecommitdiff
path: root/android/view/ShadowPainter.java
blob: 788c6c35332b2b56742e82ccd45192a2361c521e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.view;

import android.annotation.NonNull;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

public class ShadowPainter {

    /**
     * Adds a drop shadow to a semi-transparent image (of an arbitrary shape) and returns it as a
     * new image. This method attempts to mimic the same visual characteristics as the rectangular
     * shadow painting methods in this class, {@link #createRectangularDropShadow(java.awt.image.BufferedImage)}
     * and {@link #createSmallRectangularDropShadow(java.awt.image.BufferedImage)}.
     * <p/>
     * If shadowSize is less or equals to 1, no shadow will be painted and the source image will be
     * returned instead.
     *
     * @param source the source image
     * @param shadowSize the size of the shadow, normally {@link #SHADOW_SIZE or {@link
     * #SMALL_SHADOW_SIZE}}
     * @param alpha alpha value to apply to the shadow
     *
     * @return an image with the shadow painted in or the source image if shadowSize <= 1
     */
    @NonNull
    public static BufferedImage createDropShadow(BufferedImage source, int shadowSize, float
            alpha) {
        shadowSize /= 2; // make shadow size have the same meaning as in the other shadow paint methods in this class

        return createDropShadow(source, shadowSize, 0.7f * alpha, 0);
    }

    /**
     * Creates a drop shadow of a given image and returns a new image which shows the input image on
     * top of its drop shadow.
     * <p/>
     * <b>NOTE: If the shape is rectangular and opaque, consider using {@link
     * #drawRectangleShadow(Graphics2D, int, int, int, int)} instead.</b>
     *
     * @param source the source image to be shadowed
     * @param shadowSize the size of the shadow in pixels
     * @param shadowOpacity the opacity of the shadow, with 0=transparent and 1=opaque
     * @param shadowRgb the RGB int to use for the shadow color
     *
     * @return a new image with the source image on top of its shadow when shadowSize > 0 or the
     * source image otherwise
     */
    @SuppressWarnings({"SuspiciousNameCombination", "UnnecessaryLocalVariable"})  // Imported code
    public static BufferedImage createDropShadow(BufferedImage source, int shadowSize,
            float shadowOpacity, int shadowRgb) {
        if (shadowSize <= 0) {
            return source;
        }

        // This code is based on
        //      http://www.jroller.com/gfx/entry/non_rectangular_shadow

        BufferedImage image;
        int width = source.getWidth();
        int height = source.getHeight();
        image = new BufferedImage(width + SHADOW_SIZE, height + SHADOW_SIZE,
                BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2 = image.createGraphics();
        g2.drawImage(image, shadowSize, shadowSize, null);

        int dstWidth = image.getWidth();
        int dstHeight = image.getHeight();

        int left = (shadowSize - 1) >> 1;
        int right = shadowSize - left;
        int xStart = left;
        int xStop = dstWidth - right;
        int yStart = left;
        int yStop = dstHeight - right;

        shadowRgb &= 0x00FFFFFF;

        int[] aHistory = new int[shadowSize];
        int historyIdx;

        int aSum;

        int[] dataBuffer = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
        int lastPixelOffset = right * dstWidth;
        float sumDivider = shadowOpacity / shadowSize;

        // horizontal pass
        for (int y = 0, bufferOffset = 0; y < dstHeight; y++, bufferOffset = y * dstWidth) {
            aSum = 0;
            historyIdx = 0;
            for (int x = 0; x < shadowSize; x++, bufferOffset++) {
                int a = dataBuffer[bufferOffset] >>> 24;
                aHistory[x] = a;
                aSum += a;
            }

            bufferOffset -= right;

            for (int x = xStart; x < xStop; x++, bufferOffset++) {
                int a = (int) (aSum * sumDivider);
                dataBuffer[bufferOffset] = a << 24 | shadowRgb;

                // subtract the oldest pixel from the sum
                aSum -= aHistory[historyIdx];

                // get the latest pixel
                a = dataBuffer[bufferOffset + right] >>> 24;
                aHistory[historyIdx] = a;
                aSum += a;

                if (++historyIdx >= shadowSize) {
                    historyIdx -= shadowSize;
                }
            }
        }
        // vertical pass
        for (int x = 0, bufferOffset = 0; x < dstWidth; x++, bufferOffset = x) {
            aSum = 0;
            historyIdx = 0;
            for (int y = 0; y < shadowSize; y++, bufferOffset += dstWidth) {
                int a = dataBuffer[bufferOffset] >>> 24;
                aHistory[y] = a;
                aSum += a;
            }

            bufferOffset -= lastPixelOffset;

            for (int y = yStart; y < yStop; y++, bufferOffset += dstWidth) {
                int a = (int) (aSum * sumDivider);
                dataBuffer[bufferOffset] = a << 24 | shadowRgb;

                // subtract the oldest pixel from the sum
                aSum -= aHistory[historyIdx];

                // get the latest pixel
                a = dataBuffer[bufferOffset + lastPixelOffset] >>> 24;
                aHistory[historyIdx] = a;
                aSum += a;

                if (++historyIdx >= shadowSize) {
                    historyIdx -= shadowSize;
                }
            }
        }

        g2.drawImage(source, null, 0, 0);
        g2.dispose();

        return image;
    }

    /**
     * Draws a rectangular drop shadow (of size {@link #SHADOW_SIZE} by {@link #SHADOW_SIZE} around
     * the given source and returns a new image with both combined
     *
     * @param source the source image
     *
     * @return the source image with a drop shadow on the bottom and right
     */
    @SuppressWarnings("UnusedDeclaration")
    public static BufferedImage createRectangularDropShadow(BufferedImage source) {
        int type = source.getType();
        if (type == BufferedImage.TYPE_CUSTOM) {
            type = BufferedImage.TYPE_INT_ARGB;
        }

        int width = source.getWidth();
        int height = source.getHeight();
        BufferedImage image;
        image = new BufferedImage(width + SHADOW_SIZE, height + SHADOW_SIZE, type);
        Graphics2D g = image.createGraphics();
        g.drawImage(source, 0, 0, null);
        drawRectangleShadow(image, 0, 0, width, height);
        g.dispose();

        return image;
    }

    /**
     * Draws a small rectangular drop shadow (of size {@link #SMALL_SHADOW_SIZE} by {@link
     * #SMALL_SHADOW_SIZE} around the given source and returns a new image with both combined
     *
     * @param source the source image
     *
     * @return the source image with a drop shadow on the bottom and right
     */
    @SuppressWarnings("UnusedDeclaration")
    public static BufferedImage createSmallRectangularDropShadow(BufferedImage source) {
        int type = source.getType();
        if (type == BufferedImage.TYPE_CUSTOM) {
            type = BufferedImage.TYPE_INT_ARGB;
        }

        int width = source.getWidth();
        int height = source.getHeight();

        BufferedImage image;
        image = new BufferedImage(width + SMALL_SHADOW_SIZE, height + SMALL_SHADOW_SIZE, type);

        Graphics2D g = image.createGraphics();
        g.drawImage(source, 0, 0, null);
        drawSmallRectangleShadow(image, 0, 0, width, height);
        g.dispose();

        return image;
    }

    /**
     * Draws a drop shadow for the given rectangle into the given context. It will not draw anything
     * if the rectangle is smaller than a minimum determined by the assets used to draw the shadow
     * graphics. The size of the shadow is {@link #SHADOW_SIZE}.
     *
     * @param image the image to draw the shadow into
     * @param x the left coordinate of the left hand side of the rectangle
     * @param y the top coordinate of the top of the rectangle
     * @param width the width of the rectangle
     * @param height the height of the rectangle
     */
    public static void drawRectangleShadow(BufferedImage image,
            int x, int y, int width, int height) {
        Graphics2D gc = image.createGraphics();
        try {
            drawRectangleShadow(gc, x, y, width, height);
        } finally {
            gc.dispose();
        }
    }

    /**
     * Draws a small drop shadow for the given rectangle into the given context. It will not draw
     * anything if the rectangle is smaller than a minimum determined by the assets used to draw the
     * shadow graphics. The size of the shadow is {@link #SMALL_SHADOW_SIZE}.
     *
     * @param image the image to draw the shadow into
     * @param x the left coordinate of the left hand side of the rectangle
     * @param y the top coordinate of the top of the rectangle
     * @param width the width of the rectangle
     * @param height the height of the rectangle
     */
    public static void drawSmallRectangleShadow(BufferedImage image,
            int x, int y, int width, int height) {
        Graphics2D gc = image.createGraphics();
        try {
            drawSmallRectangleShadow(gc, x, y, width, height);
        } finally {
            gc.dispose();
        }
    }

    /**
     * The width and height of the drop shadow painted by
     * {@link #drawRectangleShadow(Graphics2D, int, int, int, int)}
     */
    public static final int SHADOW_SIZE = 20; // DO NOT EDIT. This corresponds to bitmap graphics

    /**
     * The width and height of the drop shadow painted by
     * {@link #drawSmallRectangleShadow(Graphics2D, int, int, int, int)}
     */
    public static final int SMALL_SHADOW_SIZE = 10; // DO NOT EDIT. Corresponds to bitmap graphics

    /**
     * Draws a drop shadow for the given rectangle into the given context. It will not draw anything
     * if the rectangle is smaller than a minimum determined by the assets used to draw the shadow
     * graphics.
     *
     * @param gc the graphics context to draw into
     * @param x the left coordinate of the left hand side of the rectangle
     * @param y the top coordinate of the top of the rectangle
     * @param width the width of the rectangle
     * @param height the height of the rectangle
     */
    public static void drawRectangleShadow(Graphics2D gc, int x, int y, int width, int height) {
        assert ShadowBottomLeft != null;
        assert ShadowBottomRight.getWidth(null) == SHADOW_SIZE;
        assert ShadowBottomRight.getHeight(null) == SHADOW_SIZE;

        int blWidth = ShadowBottomLeft.getWidth(null);
        int trHeight = ShadowTopRight.getHeight(null);
        if (width < blWidth) {
            return;
        }
        if (height < trHeight) {
            return;
        }

        gc.drawImage(ShadowBottomLeft, x - ShadowBottomLeft.getWidth(null), y + height, null);
        gc.drawImage(ShadowBottomRight, x + width, y + height, null);
        gc.drawImage(ShadowTopRight, x + width, y, null);
        gc.drawImage(ShadowTopLeft, x - ShadowTopLeft.getWidth(null), y, null);
        gc.drawImage(ShadowBottom,
                x, y + height, x + width, y + height + ShadowBottom.getHeight(null),
                0, 0, ShadowBottom.getWidth(null), ShadowBottom.getHeight(null), null);
        gc.drawImage(ShadowRight,
                x + width, y + ShadowTopRight.getHeight(null), x + width + ShadowRight.getWidth(null), y + height,
                0, 0, ShadowRight.getWidth(null), ShadowRight.getHeight(null), null);
        gc.drawImage(ShadowLeft,
                x - ShadowLeft.getWidth(null), y + ShadowTopLeft.getHeight(null), x, y + height,
                0, 0, ShadowLeft.getWidth(null), ShadowLeft.getHeight(null), null);
    }

    /**
     * Draws a small drop shadow for the given rectangle into the given context. It will not draw
     * anything if the rectangle is smaller than a minimum determined by the assets used to draw the
     * shadow graphics.
     * <p/>
     *
     * @param gc the graphics context to draw into
     * @param x the left coordinate of the left hand side of the rectangle
     * @param y the top coordinate of the top of the rectangle
     * @param width the width of the rectangle
     * @param height the height of the rectangle
     */
    public static void drawSmallRectangleShadow(Graphics2D gc, int x, int y, int width,
            int height) {
        assert Shadow2BottomLeft != null;
        assert Shadow2TopRight != null;
        assert Shadow2BottomRight.getWidth(null) == SMALL_SHADOW_SIZE;
        assert Shadow2BottomRight.getHeight(null) == SMALL_SHADOW_SIZE;

        int blWidth = Shadow2BottomLeft.getWidth(null);
        int trHeight = Shadow2TopRight.getHeight(null);
        if (width < blWidth) {
            return;
        }
        if (height < trHeight) {
            return;
        }

        gc.drawImage(Shadow2BottomLeft, x - Shadow2BottomLeft.getWidth(null), y + height, null);
        gc.drawImage(Shadow2BottomRight, x + width, y + height, null);
        gc.drawImage(Shadow2TopRight, x + width, y, null);
        gc.drawImage(Shadow2TopLeft, x - Shadow2TopLeft.getWidth(null), y, null);
        gc.drawImage(Shadow2Bottom,
                x, y + height, x + width, y + height + Shadow2Bottom.getHeight(null),
                0, 0, Shadow2Bottom.getWidth(null), Shadow2Bottom.getHeight(null), null);
        gc.drawImage(Shadow2Right,
                x + width, y + Shadow2TopRight.getHeight(null), x + width + Shadow2Right.getWidth(null), y + height,
                0, 0, Shadow2Right.getWidth(null), Shadow2Right.getHeight(null), null);
        gc.drawImage(Shadow2Left,
                x - Shadow2Left.getWidth(null), y + Shadow2TopLeft.getHeight(null), x, y + height,
                0, 0, Shadow2Left.getWidth(null), Shadow2Left.getHeight(null), null);
    }

    private static Image loadIcon(String name) {
        InputStream inputStream = ShadowPainter.class.getResourceAsStream(name);
        if (inputStream == null) {
            throw new RuntimeException("Unable to load image for shadow: " + name);
        }
        try {
            return ImageIO.read(inputStream);
        } catch (IOException e) {
            throw new RuntimeException("Unable to load image for shadow:" + name, e);
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                // ignore.
            }
        }
    }

    // Shadow graphics. This was generated by creating a drop shadow in
    // Gimp, using the parameters x offset=10, y offset=10, blur radius=10,
    // (for the small drop shadows x offset=10, y offset=10, blur radius=10)
    // color=black, and opacity=51. These values attempt to make a shadow
    // that is legible both for dark and light themes, on top of the
    // canvas background (rgb(150,150,150). Darker shadows would tend to
    // blend into the foreground for a dark holo screen, and lighter shadows
    // would be hard to spot on the canvas background. If you make adjustments,
    // make sure to check the shadow with both dark and light themes.
    //
    // After making the graphics, I cut out the top right, bottom left
    // and bottom right corners as 20x20 images, and these are reproduced by
    // painting them in the corresponding places in the target graphics context.
    // I then grabbed a single horizontal gradient line from the middle of the
    // right edge,and a single vertical gradient line from the bottom. These
    // are then painted scaled/stretched in the target to fill the gaps between
    // the three corner images.
    //
    // Filenames: bl=bottom left, b=bottom, br=bottom right, r=right, tr=top right

    // Normal Drop Shadow
    private static final Image ShadowBottom = loadIcon("/icons/shadow-b.png");
    private static final Image ShadowBottomLeft = loadIcon("/icons/shadow-bl.png");
    private static final Image ShadowBottomRight = loadIcon("/icons/shadow-br.png");
    private static final Image ShadowRight = loadIcon("/icons/shadow-r.png");
    private static final Image ShadowTopRight = loadIcon("/icons/shadow-tr.png");
    private static final Image ShadowTopLeft = loadIcon("/icons/shadow-tl.png");
    private static final Image ShadowLeft = loadIcon("/icons/shadow-l.png");

    // Small Drop Shadow
    private static final Image Shadow2Bottom = loadIcon("/icons/shadow2-b.png");
    private static final Image Shadow2BottomLeft = loadIcon("/icons/shadow2-bl.png");
    private static final Image Shadow2BottomRight = loadIcon("/icons/shadow2-br.png");
    private static final Image Shadow2Right = loadIcon("/icons/shadow2-r.png");
    private static final Image Shadow2TopRight = loadIcon("/icons/shadow2-tr.png");
    private static final Image Shadow2TopLeft = loadIcon("/icons/shadow2-tl.png");
    private static final Image Shadow2Left = loadIcon("/icons/shadow2-l.png");
}