OSDN Git Service

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