Home | History | Annotate | Download | only in assetstudiolib
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
      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 package com.android.assetstudiolib;
     17 
     18 import com.android.assetstudiolib.Util.Effect;
     19 import com.android.assetstudiolib.Util.FillEffect;
     20 import com.android.assetstudiolib.Util.ShadowEffect;
     21 
     22 import java.awt.Color;
     23 import java.awt.Graphics2D;
     24 import java.awt.Rectangle;
     25 import java.awt.image.BufferedImage;
     26 
     27 /**
     28  * Generate icons for the action bar
     29  */
     30 public class ActionBarIconGenerator extends GraphicGenerator {
     31 
     32     /** Creates a new {@link ActionBarIconGenerator} */
     33     public ActionBarIconGenerator() {
     34     }
     35 
     36     @Override
     37     public BufferedImage generate(GraphicGeneratorContext context, Options options) {
     38         Rectangle iconSizeHdpi = new Rectangle(0, 0, 48, 48);
     39         Rectangle targetRectHdpi = new Rectangle(6, 6, 36, 36);
     40         final float scaleFactor = GraphicGenerator.getHdpiScaleFactor(options.density);
     41         Rectangle imageRect = Util.scaleRectangle(iconSizeHdpi, scaleFactor);
     42         Rectangle targetRect = Util.scaleRectangle(targetRectHdpi, scaleFactor);
     43         BufferedImage outImage = Util.newArgbBufferedImage(imageRect.width, imageRect.height);
     44         Graphics2D g = (Graphics2D) outImage.getGraphics();
     45 
     46         BufferedImage tempImage = Util.newArgbBufferedImage(
     47                 imageRect.width, imageRect.height);
     48         Graphics2D g2 = (Graphics2D) tempImage.getGraphics();
     49         Util.drawCenterInside(g2, options.sourceImage, targetRect);
     50 
     51         ActionBarOptions actionBarOptions = (ActionBarOptions) options;
     52         if (actionBarOptions.theme == Theme.HOLO_LIGHT) {
     53             Util.drawEffects(g, tempImage, 0, 0, new Effect[] {
     54                     new FillEffect(new Color(0x898989)),
     55             });
     56         } else {
     57             assert actionBarOptions.theme == Theme.HOLO_DARK;
     58             Util.drawEffects(g, tempImage, 0, 0, new Effect[] {
     59                     // TODO: should be white @ 60% opacity, but
     60                     // the fill then blends with the drop shadow
     61                     new FillEffect(new Color(0x909090)),
     62                     new ShadowEffect(
     63                             0,
     64                             0,
     65                             3 * scaleFactor,
     66                             Color.BLACK,
     67                             0.85,
     68                             false),
     69             });
     70         }
     71 
     72         g.dispose();
     73         g2.dispose();
     74 
     75         return outImage;
     76     }
     77 
     78     /** Options specific to generating action bar icons */
     79     public static class ActionBarOptions extends GraphicGenerator.Options {
     80         /** The theme to generate icons for */
     81         public Theme theme = Theme.HOLO_LIGHT;
     82     }
     83 
     84     /** The themes to generate action bar icons for */
     85     public enum Theme {
     86         /** Theme.Holo - a dark (and default) version of the Honeycomb theme */
     87         HOLO_DARK,
     88 
     89         /** Theme.HoloLight - a light version of the Honeycomb theme */
     90         HOLO_LIGHT;
     91     }
     92 }
     93