Example usage for android.graphics Picture beginRecording

List of usage examples for android.graphics Picture beginRecording

Introduction

In this page you can find the example usage for android.graphics Picture beginRecording.

Prototype

public Canvas beginRecording(int width, int height) 

Source Link

Document

To record a picture, call beginRecording() and then draw into the Canvas that is returned.

Usage

From source file:com.crs4.roodin.moduletester.CustomView.java

/**
 * @return//  ww w . j a v a 2 s .  co m
 */
private Picture drawBarreds() {
    Picture pictureContent = new Picture();
    Canvas canvasContent = pictureContent.beginRecording(getWidth(), getHeight());
    Paint paintContent = new Paint(Paint.ANTI_ALIAS_FLAG);
    canvasContent.save();

    paintContent.setColor(Color.BLUE);
    paintContent.setStyle(Style.STROKE);
    paintContent.setStrokeWidth(2);
    paintContent.setAlpha(50);

    try {
        cellDimension = block.getCellDimension();
        JSONArray barred = block.getBarred();

        int rows = block.getShape().getInt(0);
        int cols = block.getShape().getInt(1);

        // this code draws all the shape:
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {

                canvasContent.drawRect(cellDimension * j, cellDimension * i, cellDimension * j + cellDimension,
                        cellDimension * i + cellDimension, paintContent);
            }
        }
        paintContent.setColor(Color.RED);
        paintContent.setStyle(Style.FILL);
        paintContent.setAlpha(50);

        for (int i = 0; i < barred.length(); i++) {
            JSONArray pos = barred.getJSONArray(i);
            canvasContent.drawRect(cellDimension * pos.getInt(1), cellDimension * pos.getInt(0),
                    cellDimension * pos.getInt(1) + cellDimension,
                    cellDimension * pos.getInt(0) + cellDimension, paintContent);
        }

        this.paintBoxList(canvasContent, paintContent);

    } catch (JSONException e) {
        e.printStackTrace();
        Log.e("Exception in DrawComponents.drawBarreds", "" + e.getMessage());
    }

    canvasContent.restore();
    pictureContent.endRecording();
    return pictureContent;

}

From source file:com.google.appinventor.components.runtime.util.NativeOpenStreetMapController.java

private Drawable rasterizeSVG(MapMarker aiMarker, SVG markerSvg) {
    SVG.Svg svg = markerSvg.getRootElement();
    final float density = view.getContext().getResources().getDisplayMetrics().density;
    float height = aiMarker.Height() <= 0 ? getBestGuessHeight(svg) : aiMarker.Height();
    float width = aiMarker.Width() <= 0 ? getBestGuessWidth(svg) : aiMarker.Width();
    float scaleH = height / getBestGuessHeight(svg);
    float scaleW = width / getBestGuessWidth(svg);
    float scale = (float) Math.sqrt(scaleH * scaleH + scaleW * scaleW);

    // update fill color of SVG <path>
    Paint fillPaint = new Paint();
    Paint strokePaint = new Paint();
    PaintUtil.changePaint(fillPaint, aiMarker.FillColor());
    PaintUtil.changePaint(strokePaint, aiMarker.StrokeColor());
    SVG.Length strokeWidth = new SVG.Length(aiMarker.StrokeWidth() / scale);
    for (SVG.SvgObject element : svg.getChildren()) {
        if (element instanceof SVG.SvgConditionalElement) {
            SVG.SvgConditionalElement path = (SVG.SvgConditionalElement) element;
            path.baseStyle.fill = new SVG.Colour(fillPaint.getColor());
            path.baseStyle.fillOpacity = fillPaint.getAlpha() / 255.0f;
            path.baseStyle.stroke = new SVG.Colour(strokePaint.getColor());
            path.baseStyle.strokeOpacity = strokePaint.getAlpha() / 255.0f;
            path.baseStyle.strokeWidth = strokeWidth;
            if (path.style != null) {
                if ((path.style.specifiedFlags & SPECIFIED_FILL) == 0) {
                    path.style.fill = new SVG.Colour(fillPaint.getColor());
                    path.style.specifiedFlags |= SPECIFIED_FILL;
                }/* www  . j a  v  a  2  s. co  m*/
                if ((path.style.specifiedFlags & SPECIFIED_FILL_OPACITY) == 0) {
                    path.style.fillOpacity = fillPaint.getAlpha() / 255.0f;
                    path.style.specifiedFlags |= SPECIFIED_FILL_OPACITY;
                }
                if ((path.style.specifiedFlags & SPECIFIED_STROKE) == 0) {
                    path.style.stroke = new SVG.Colour(strokePaint.getColor());
                    path.style.specifiedFlags |= SPECIFIED_STROKE;
                }
                if ((path.style.specifiedFlags & SPECIFIED_STROKE_OPACITY) == 0) {
                    path.style.strokeOpacity = strokePaint.getAlpha() / 255.0f;
                    path.style.specifiedFlags |= SPECIFIED_STROKE_OPACITY;
                }
                if ((path.style.specifiedFlags & SPECIFIED_STROKE_WIDTH) == 0) {
                    path.style.strokeWidth = strokeWidth;
                    path.style.specifiedFlags |= SPECIFIED_STROKE_WIDTH;
                }
            }
        }
    }

    // draw SVG to Picture and create a BitmapDrawable for rendering
    Picture picture = markerSvg.renderToPicture();
    Picture scaledPicture = new Picture();
    Canvas canvas = scaledPicture.beginRecording((int) ((width + 2.0f * aiMarker.StrokeWidth()) * density),
            (int) ((height + 2.0f * aiMarker.StrokeWidth()) * density));
    canvas.scale(density * scaleW, density * scaleH);
    canvas.translate(strokeWidth.floatValue(), strokeWidth.floatValue());
    picture.draw(canvas);
    scaledPicture.endRecording();
    return new PictureDrawable(scaledPicture);
}