Search in sources :

Example 6 with DrawableParams

use of com.android.ide.common.rendering.api.DrawableParams in project android_frameworks_base by ParanoidAndroid.

the class RenderDrawable method render.

public Result render() {
    checkLock();
    try {
        // 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);
    } catch (IOException e) {
        return ERROR_UNKNOWN.createResult(e.getMessage(), e);
    }
}
Also used : Bitmap(android.graphics.Bitmap) HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig) FrameLayout(android.widget.FrameLayout) Canvas(android.graphics.Canvas) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) Drawable(android.graphics.drawable.Drawable) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) DrawableParams(com.android.ide.common.rendering.api.DrawableParams)

Aggregations

Drawable (android.graphics.drawable.Drawable)6 DrawableParams (com.android.ide.common.rendering.api.DrawableParams)6 HardwareConfig (com.android.ide.common.rendering.api.HardwareConfig)6 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)6 BridgeContext (com.android.layoutlib.bridge.android.BridgeContext)6 BufferedImage (java.awt.image.BufferedImage)6 StateListDrawable (android.graphics.drawable.StateListDrawable)5 ResourceType (com.android.resources.ResourceType)5 Bitmap (android.graphics.Bitmap)1 Canvas (android.graphics.Canvas)1 FrameLayout (android.widget.FrameLayout)1 IOException (java.io.IOException)1