• 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 java.awt.AlphaComposite;
20 import java.awt.Color;
21 import java.awt.Graphics2D;
22 import java.awt.Rectangle;
23 import java.awt.image.BufferedImage;
24 import java.util.HashMap;
25 import java.util.Map;
26 
27 /**
28  * A {@link GraphicGenerator} that generates Android "launcher" icons.
29  */
30 public class LauncherIconGenerator extends GraphicGenerator {
31     private static final Rectangle IMAGE_SIZE_MDPI = new Rectangle(0, 0, 48, 48);
32     private static final Rectangle TARGET_RECT_MDPI = new Rectangle(2, 2, 44, 44);
33 
34     @Override
generate(GraphicGeneratorContext context, Options options)35     public BufferedImage generate(GraphicGeneratorContext context, Options options) {
36         LauncherOptions launcherOptions = (LauncherOptions) options;
37 
38         String density;
39         if (launcherOptions.isWebGraphic) {
40             density = "web";
41         } else {
42             density = launcherOptions.density.getResourceValue();
43         }
44         String shape = launcherOptions.shape.id;
45         BufferedImage mBackImage = context.loadImageResource("/images/launcher_stencil/"
46                 + shape + "/" + density + "/back.png");
47         BufferedImage mForeImage = context.loadImageResource("/images/launcher_stencil/"
48                 + shape + "/" + density + "/" + launcherOptions.style.id + ".png");
49         BufferedImage mMaskImage = context.loadImageResource("/images/launcher_stencil/"
50                 + shape + "/" + density + "/mask.png");
51 
52         float scaleFactor = GraphicGenerator.getMdpiScaleFactor(launcherOptions.density);
53         if (launcherOptions.isWebGraphic) {
54             // Target size for the web graphic is 512
55             scaleFactor = 512 / (float) IMAGE_SIZE_MDPI.height;
56         }
57         Rectangle imageRect = Util.scaleRectangle(IMAGE_SIZE_MDPI, scaleFactor);
58         Rectangle targetRect = Util.scaleRectangle(TARGET_RECT_MDPI, scaleFactor);
59 
60         BufferedImage outImage = Util.newArgbBufferedImage(imageRect.width, imageRect.height);
61         Graphics2D g = (Graphics2D) outImage.getGraphics();
62         g.drawImage(mBackImage, 0, 0, null);
63 
64         BufferedImage tempImage = Util.newArgbBufferedImage(imageRect.width, imageRect.height);
65         Graphics2D g2 = (Graphics2D) tempImage.getGraphics();
66         g2.drawImage(mMaskImage, 0, 0, null);
67         g2.setComposite(AlphaComposite.SrcAtop);
68         g2.setPaint(new Color(launcherOptions.backgroundColor));
69         g2.fillRect(0, 0, imageRect.width, imageRect.height);
70 
71         if (launcherOptions.crop) {
72             Util.drawCenterCrop(g2, launcherOptions.sourceImage, targetRect);
73         } else {
74             Util.drawCenterInside(g2, launcherOptions.sourceImage, targetRect);
75         }
76 
77         g.drawImage(tempImage, 0, 0, null);
78         g.drawImage(mForeImage, 0, 0, null);
79 
80         g.dispose();
81         g2.dispose();
82 
83         return outImage;
84     }
85 
86     @Override
generate(String category, Map<String, Map<String, BufferedImage>> categoryMap, GraphicGeneratorContext context, Options options, String name)87     public void generate(String category, Map<String, Map<String, BufferedImage>> categoryMap,
88             GraphicGeneratorContext context, Options options, String name) {
89         LauncherOptions launcherOptions = (LauncherOptions) options;
90         boolean generateWebImage = launcherOptions.isWebGraphic;
91         launcherOptions.isWebGraphic = false;
92         super.generate(category, categoryMap, context, options, name);
93 
94         if (generateWebImage) {
95             launcherOptions.isWebGraphic = true;
96             BufferedImage image = generate(context, options);
97             if (image != null) {
98                 Map<String, BufferedImage> imageMap = new HashMap<String, BufferedImage>();
99                 categoryMap.put("Web", imageMap);
100                 imageMap.put(getIconPath(options, name), image);
101             }
102         }
103     }
104 
105     @Override
getIconPath(Options options, String name)106     protected String getIconPath(Options options, String name) {
107         if (((LauncherOptions) options).isWebGraphic) {
108             return name + "-web.png"; // Store at the root of the project
109         }
110 
111         return super.getIconPath(options, name);
112     }
113 
114     /** Options specific to generating launcher icons */
115     public static class LauncherOptions extends GraphicGenerator.Options {
116         /** Background color, as an RRGGBB packed integer */
117         public int backgroundColor = 0;
118 
119         /** Whether the image should be cropped or not */
120         public boolean crop = true;
121 
122         /** The shape to use for the background */
123         public Shape shape = Shape.SQUARE;
124 
125         /** The effects to apply to the foreground */
126         public Style style = Style.SIMPLE;
127 
128         /**
129          * Whether a web graphic should be generated (will ignore normal density
130          * setting). The {@link #generate(GraphicGeneratorContext, Options)}
131          * method will use this to decide whether to generate a normal density
132          * icon or a high res web image. The
133          * {@link GraphicGenerator#generate(String, Map, GraphicGeneratorContext, Options, String)}
134          * method will use this flag to determine whether it should include a
135          * web graphic in its iteration.
136          */
137         public boolean isWebGraphic;
138     }
139 }
140