RenderDrawable.java revision fd18f573280bbbcc549b35b548580a562bd960e2
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.ResourceValue; 23import com.android.ide.common.rendering.api.Result; 24import com.android.ide.common.rendering.api.Result.Status; 25import com.android.layoutlib.bridge.android.BridgeContext; 26import com.android.layoutlib.bridge.android.BridgeWindow; 27import com.android.layoutlib.bridge.android.BridgeWindowSession; 28import com.android.resources.ResourceType; 29 30import android.graphics.Bitmap; 31import android.graphics.Bitmap_Delegate; 32import android.graphics.Canvas; 33import android.graphics.drawable.Drawable; 34import android.os.Handler; 35import android.view.View; 36import android.view.View.AttachInfo; 37import android.view.View.MeasureSpec; 38import android.widget.FrameLayout; 39 40import java.awt.AlphaComposite; 41import java.awt.Color; 42import java.awt.Graphics2D; 43import java.awt.image.BufferedImage; 44import 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 */ 55public 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 info.mHardwareAccelerated = false; 91 content.dispatchAttachedToWindow(info, 0); 92 93 94 // measure 95 int w = params.getScreenWidth(); 96 int h = params.getScreenHeight(); 97 int w_spec = MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY); 98 int h_spec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY); 99 content.measure(w_spec, h_spec); 100 101 // now do the layout. 102 content.layout(0, 0, w, h); 103 104 // preDraw setup 105 content.mAttachInfo.mTreeObserver.dispatchOnPreDraw(); 106 107 // draw into a new image 108 BufferedImage image = getImage(w, h); 109 110 // create an Android bitmap around the BufferedImage 111 Bitmap bitmap = Bitmap_Delegate.createBitmap(image, 112 true /*isMutable*/, params.getDensity()); 113 114 // create a Canvas around the Android bitmap 115 Canvas canvas = new Canvas(bitmap); 116 canvas.setDensity(params.getDensity().getDpiValue()); 117 118 // and draw 119 content.draw(canvas); 120 121 return Status.SUCCESS.createResult(image); 122 } catch (IOException e) { 123 return ERROR_UNKNOWN.createResult(e.getMessage(), e); 124 } 125 } 126 127 protected BufferedImage getImage(int w, int h) { 128 BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 129 Graphics2D gc = image.createGraphics(); 130 gc.setComposite(AlphaComposite.Src); 131 132 gc.setColor(new Color(0x00000000, true)); 133 gc.fillRect(0, 0, w, h); 134 135 // done 136 gc.dispose(); 137 138 return image; 139 } 140 141} 142