FileDocCategorySizeDatePackage
RenderDrawable.javaAPI DocAndroid 5.1 API4341Thu Mar 12 22:22:44 GMT 2015com.android.layoutlib.bridge.impl

RenderDrawable

public class RenderDrawable extends RenderAction
Action to render a given Drawable provided through {@link DrawableParams#getDrawable()}. The class only provides a simple {@link #render()} method, but the full life-cycle of the action must be respected.
see
RenderAction

Fields Summary
Constructors Summary
public RenderDrawable(com.android.ide.common.rendering.api.DrawableParams params)

        super(new DrawableParams(params));
    
Methods Summary
protected java.awt.image.BufferedImagegetImage(int w, int h)

        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gc = image.createGraphics();
        gc.setComposite(AlphaComposite.Src);

        gc.setColor(new Color(0x00000000, true));
        gc.fillRect(0, 0, w, h);

        // done
        gc.dispose();

        return image;
    
public com.android.ide.common.rendering.api.Resultrender()

        checkLock();
        // get the drawable resource value
        DrawableParams params = getParams();
        HardwareConfig hardwareConfig = params.getHardwareConfig();
        ResourceValue drawableResource = params.getDrawable();

        // resolve it
        BridgeContext context = getContext();
        drawableResource = context.getRenderResources().resolveResValue(drawableResource);

        if (drawableResource == null ||
                drawableResource.getResourceType() != ResourceType.DRAWABLE) {
            return Status.ERROR_NOT_A_DRAWABLE.createResult();
        }

        // create a simple FrameLayout
        FrameLayout content = new FrameLayout(context);

        // get the actual Drawable object to draw
        Drawable d = ResourceHelper.getDrawable(drawableResource, context);
        content.setBackground(d);

        // set the AttachInfo on the root view.
        AttachInfo_Accessor.setAttachInfo(content);


        // measure
        int w = hardwareConfig.getScreenWidth();
        int h = hardwareConfig.getScreenHeight();
        int w_spec = MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY);
        int h_spec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
        content.measure(w_spec, h_spec);

        // now do the layout.
        content.layout(0, 0, w, h);

        // preDraw setup
        AttachInfo_Accessor.dispatchOnPreDraw(content);

        // draw into a new image
        BufferedImage image = getImage(w, h);

        // create an Android bitmap around the BufferedImage
        Bitmap bitmap = Bitmap_Delegate.createBitmap(image,
                true /*isMutable*/, hardwareConfig.getDensity());

        // create a Canvas around the Android bitmap
        Canvas canvas = new Canvas(bitmap);
        canvas.setDensity(hardwareConfig.getDensity().getDpiValue());

        // and draw
        content.draw(canvas);

        return Status.SUCCESS.createResult(image);