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
17package com.android.layoutlib.bridge.impl;
18
19import static com.android.ide.common.rendering.api.Result.Status.ERROR_UNKNOWN;
20
21import com.android.ide.common.rendering.api.DrawableParams;
22import com.android.ide.common.rendering.api.HardwareConfig;
23import com.android.ide.common.rendering.api.ResourceValue;
24import com.android.ide.common.rendering.api.Result;
25import com.android.ide.common.rendering.api.Result.Status;
26import com.android.layoutlib.bridge.android.BridgeContext;
27import com.android.resources.ResourceType;
28
29import android.graphics.Bitmap;
30import android.graphics.Bitmap_Delegate;
31import android.graphics.Canvas;
32import android.graphics.drawable.Drawable;
33import android.view.AttachInfo_Accessor;
34import android.view.View.MeasureSpec;
35import android.widget.FrameLayout;
36
37import java.awt.AlphaComposite;
38import java.awt.Color;
39import java.awt.Graphics2D;
40import java.awt.image.BufferedImage;
41import java.io.IOException;
42
43/**
44 * Action to render a given Drawable provided through {@link DrawableParams#getDrawable()}.
45 *
46 * The class only provides a simple {@link #render()} method, but the full life-cycle of the
47 * action must be respected.
48 *
49 * @see RenderAction
50 *
51 */
52public class RenderDrawable extends RenderAction<DrawableParams> {
53
54    public RenderDrawable(DrawableParams params) {
55        super(new DrawableParams(params));
56    }
57
58    public Result render() {
59        checkLock();
60        // get the drawable resource value
61        DrawableParams params = getParams();
62        HardwareConfig hardwareConfig = params.getHardwareConfig();
63        ResourceValue drawableResource = params.getDrawable();
64
65        // resolve it
66        BridgeContext context = getContext();
67        drawableResource = context.getRenderResources().resolveResValue(drawableResource);
68
69        if (drawableResource == null ||
70                drawableResource.getResourceType() != ResourceType.DRAWABLE) {
71            return Status.ERROR_NOT_A_DRAWABLE.createResult();
72        }
73
74        // create a simple FrameLayout
75        FrameLayout content = new FrameLayout(context);
76
77        // get the actual Drawable object to draw
78        Drawable d = ResourceHelper.getDrawable(drawableResource, context);
79        content.setBackground(d);
80
81        // set the AttachInfo on the root view.
82        AttachInfo_Accessor.setAttachInfo(content);
83
84
85        // measure
86        int w = hardwareConfig.getScreenWidth();
87        int h = hardwareConfig.getScreenHeight();
88        int w_spec = MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY);
89        int h_spec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
90        content.measure(w_spec, h_spec);
91
92        // now do the layout.
93        content.layout(0, 0, w, h);
94
95        // preDraw setup
96        AttachInfo_Accessor.dispatchOnPreDraw(content);
97
98        // draw into a new image
99        BufferedImage image = getImage(w, h);
100
101        // create an Android bitmap around the BufferedImage
102        Bitmap bitmap = Bitmap_Delegate.createBitmap(image,
103                true /*isMutable*/, hardwareConfig.getDensity());
104
105        // create a Canvas around the Android bitmap
106        Canvas canvas = new Canvas(bitmap);
107        canvas.setDensity(hardwareConfig.getDensity().getDpiValue());
108
109        // and draw
110        content.draw(canvas);
111
112        return Status.SUCCESS.createResult(image);
113    }
114
115    protected BufferedImage getImage(int w, int h) {
116        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
117        Graphics2D gc = image.createGraphics();
118        gc.setComposite(AlphaComposite.Src);
119
120        gc.setColor(new Color(0x00000000, true));
121        gc.fillRect(0, 0, w, h);
122
123        // done
124        gc.dispose();
125
126        return image;
127    }
128
129}
130