File: ActionBarIconGenerator.java

package info (click to toggle)
android-platform-tools-base 2.2.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 113,836 kB
  • sloc: java: 696,390; xml: 45,920; cpp: 2,526; ansic: 1,432; sh: 508; lisp: 110; javascript: 108; makefile: 17
file content (94 lines) | stat: -rw-r--r-- 3,722 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
 *
 * 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 com.android.assetstudiolib;

import com.android.ide.common.util.AssetUtil;
import com.android.ide.common.util.AssetUtil.Effect;
import com.android.ide.common.util.AssetUtil.FillEffect;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

/**
 * Generate icons for the action bar
 */
public class ActionBarIconGenerator extends GraphicGenerator {

    /** Creates a new {@link ActionBarIconGenerator} */
    public ActionBarIconGenerator() {
    }

    @Override
    public BufferedImage generate(GraphicGeneratorContext context, Options options) {
        ActionBarOptions actionBarOptions = (ActionBarOptions) options;
        Rectangle iconSizeMdpi = new Rectangle(0, 0, 32, 32);
        Rectangle targetRectMdpi = actionBarOptions.sourceIsClipart
                ? new Rectangle(0, 0, 32, 32)
                : new Rectangle(4, 4, 24, 24);
        final float scaleFactor = GraphicGenerator.getMdpiScaleFactor(options.density);
        Rectangle imageRect = AssetUtil.scaleRectangle(iconSizeMdpi, scaleFactor);
        Rectangle targetRect = AssetUtil.scaleRectangle(targetRectMdpi, scaleFactor);
        BufferedImage outImage = AssetUtil.newArgbBufferedImage(imageRect.width, imageRect.height);
        Graphics2D g = (Graphics2D) outImage.getGraphics();

        BufferedImage tempImage = AssetUtil.newArgbBufferedImage(imageRect.width, imageRect.height);
        Graphics2D g2 = (Graphics2D)tempImage.getGraphics();
        AssetUtil.drawCenterInside(g2, options.sourceImage, targetRect);

        if (actionBarOptions.theme == Theme.CUSTOM) {
            AssetUtil.drawEffects(g, tempImage, 0, 0, new Effect[]{
              new FillEffect(new Color(actionBarOptions.customThemeColor), 0.8),});
        } else if (actionBarOptions.theme == Theme.HOLO_LIGHT) {
            AssetUtil.drawEffects(g, tempImage, 0, 0,
                                  new Effect[]{new FillEffect(new Color(0x333333), 0.6),});
        } else {
            assert actionBarOptions.theme == Theme.HOLO_DARK;
            AssetUtil.drawEffects(g, tempImage, 0, 0,
                                  new Effect[]{new FillEffect(new Color(0xFFFFFF), 0.8)});
        }

        g.dispose();
        g2.dispose();

        return outImage;
    }

    /** Options specific to generating action bar icons */
    public static class ActionBarOptions extends GraphicGenerator.Options {
        /** The theme to generate icons for */
        public Theme theme = Theme.HOLO_LIGHT;

        /** Whether or not the source image is a clipart source */
        public boolean sourceIsClipart = false;

        /** Custom color for use with the custom theme */
        public int customThemeColor = 0;
    }

    /** The themes to generate action bar icons for */
    public enum Theme {
        /** Theme.Holo - a dark (and default) version of the Honeycomb theme */
        HOLO_DARK,

        /** Theme.HoloLight - a light version of the Honeycomb theme */
        HOLO_LIGHT,

        /** Theme.Custom - custom colors */
        CUSTOM
    }
}