• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 com.android.assetstudiolib;
18 
19 import com.android.assetstudiolib.Util.Effect;
20 import com.android.assetstudiolib.Util.FillEffect;
21 import com.android.assetstudiolib.Util.ShadowEffect;
22 
23 import java.awt.Color;
24 import java.awt.GradientPaint;
25 import java.awt.Graphics2D;
26 import java.awt.Rectangle;
27 import java.awt.image.BufferedImage;
28 
29 /**
30  * A {@link GraphicGenerator} that generates Android "menu" icons.
31  */
32 public class MenuIconGenerator extends GraphicGenerator {
33     /** Creates a menu icon generator */
MenuIconGenerator()34     public MenuIconGenerator() {
35     }
36 
37     @Override
generate(GraphicGeneratorContext context, Options options)38     public BufferedImage generate(GraphicGeneratorContext context, Options options) {
39         Rectangle imageSizeHdpi = new Rectangle(0, 0, 48, 48);
40         Rectangle targetRectHdpi = new Rectangle(8, 8, 32, 32);
41         float scaleFactor = GraphicGenerator.getMdpiScaleFactor(options.density);
42         Rectangle imageRect = Util.scaleRectangle(imageSizeHdpi, scaleFactor);
43         Rectangle targetRect = Util.scaleRectangle(targetRectHdpi, scaleFactor);
44 
45         BufferedImage outImage = Util.newArgbBufferedImage(imageRect.width, imageRect.height);
46         Graphics2D g = (Graphics2D) outImage.getGraphics();
47 
48         BufferedImage tempImage = Util.newArgbBufferedImage(
49                 imageRect.width, imageRect.height);
50         Graphics2D g2 = (Graphics2D) tempImage.getGraphics();
51         Util.drawCenterInside(g2, options.sourceImage, targetRect);
52 
53         Util.drawEffects(g, tempImage, 0, 0, new Effect[] {
54                 new FillEffect(
55                         new GradientPaint(
56                                 0, 0,
57                                 new Color(0xa3a3a3),
58                                 0, imageRect.height,
59                                 new Color(0x787878))),
60                 new ShadowEffect(
61                         0,
62                         2 * scaleFactor,
63                         2 * scaleFactor,
64                         Color.BLACK,
65                         0.2,
66                         true),
67                 new ShadowEffect(
68                         0,
69                         1,
70                         0,
71                         Color.BLACK,
72                         0.35,
73                         true),
74                 new ShadowEffect(
75                         0,
76                         -1,
77                         0,
78                         Color.WHITE,
79                         0.35,
80                         true),
81         });
82 
83         g.dispose();
84         g2.dispose();
85 
86         return outImage;
87     }
88 }
89