2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.layoutlib.bridge.impl;
19 import static com.android.ide.common.rendering.api.Result.Status.ERROR_UNKNOWN;
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;
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;
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;
47 * Action to render a given Drawable provided through {@link DrawableParams#getDrawable()}.
49 * The class only provides a simple {@link #render()} method, but the full life-cycle of the
50 * action must be respected.
55 public class RenderDrawable extends RenderAction<DrawableParams> {
57 public RenderDrawable(DrawableParams params) {
58 super(new DrawableParams(params));
61 public Result render() {
64 // get the drawable resource value
65 DrawableParams params = getParams();
66 ResourceValue drawableResource = params.getDrawable();
69 BridgeContext context = getContext();
70 drawableResource = context.getRenderResources().resolveResValue(drawableResource);
72 if (drawableResource == null ||
73 drawableResource.getResourceType() != ResourceType.DRAWABLE) {
74 return Status.ERROR_NOT_A_DRAWABLE.createResult();
77 // create a simple FrameLayout
78 FrameLayout content = new FrameLayout(context);
80 // get the actual Drawable object to draw
81 Drawable d = ResourceHelper.getDrawable(drawableResource, context);
82 content.setBackgroundDrawable(d);
84 // set the AttachInfo on the root view.
85 AttachInfo info = new AttachInfo(new BridgeWindowSession(), new BridgeWindow(),
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);
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);
100 // now do the layout.
101 content.layout(0, 0, w, h);
104 content.mAttachInfo.mTreeObserver.dispatchOnPreDraw();
106 // draw into a new image
107 BufferedImage image = getImage(w, h);
109 // create an Android bitmap around the BufferedImage
110 Bitmap bitmap = Bitmap_Delegate.createBitmap(image,
111 true /*isMutable*/, params.getDensity());
113 // create a Canvas around the Android bitmap
114 Canvas canvas = new Canvas(bitmap);
115 canvas.setDensity(params.getDensity().getDpiValue());
118 content.draw(canvas);
120 return Status.SUCCESS.createResult(image);
121 } catch (IOException e) {
122 return ERROR_UNKNOWN.createResult(e.getMessage(), e);
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);
131 gc.setColor(new Color(0x00000000, true));
132 gc.fillRect(0, 0, w, h);