Example usage for android.graphics Picture Picture

List of usage examples for android.graphics Picture Picture

Introduction

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

Prototype

public Picture() 

Source Link

Document

Creates an empty picture that is ready to record.

Usage

From source file:com.jiahuan.svgmapview.core.helper.map.SVGParser.java

static SVG parse(InputSource data, SVGHandler handler) throws SVGParseException {
    try {/*from w ww  .ja  va  2 s.  co  m*/
        final Picture picture = new Picture();
        handler.setPicture(picture);

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        xr.setContentHandler(handler);
        xr.setFeature("http://xml.org/sax/features/validation", false);
        if (DISALLOW_DOCTYPE_DECL) {
            try {
                xr.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
            } catch (SAXNotRecognizedException e) {
                DISALLOW_DOCTYPE_DECL = false;
            }
        }
        xr.parse(data);

        SVG result = new SVG(picture, handler.bounds);
        // Skip bounds if it was an empty pic
        if (!Float.isInfinite(handler.limits.top)) {
            result.setLimits(handler.limits);
        }
        return result;
    } catch (Exception e) {
        Log.e(TAG, "Failed to parse SVG.", e);
        throw new SVGParseException(e);
    }
}

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

/**
 * @return// www .  j a v  a2s .  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.larvalabs.svgandroid.SVGParser.java

private static SVG parse(InputStream in, Integer searchColor, Integer replaceColor, boolean whiteMode)
        throws SVGParseException {
    // Util.debug("Parsing SVG...");
    SVGHandler svgHandler = null;/*from  ww  w .  ja  v a 2s. co  m*/
    try {
        // long start = System.currentTimeMillis();
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        final Picture picture = new Picture();
        svgHandler = new SVGHandler(picture);
        svgHandler.setColorSwap(searchColor, replaceColor);
        svgHandler.setWhiteMode(whiteMode);

        CopyInputStream cin = new CopyInputStream(in);

        IDHandler idHandler = new IDHandler();
        xr.setContentHandler(idHandler);
        xr.parse(new InputSource(cin.getCopy()));
        svgHandler.idXml = idHandler.idXml;

        xr.setContentHandler(svgHandler);
        xr.parse(new InputSource(cin.getCopy()));
        // Util.debug("Parsing complete in " + (System.currentTimeMillis() - start) + " millis.");
        SVG result = new SVG(picture, svgHandler.bounds);
        // Skip bounds if it was an empty pic
        if (!Float.isInfinite(svgHandler.limits.top)) {
            result.setLimits(svgHandler.limits);
        }
        return result;
    } catch (Exception e) {
        //for (String s : handler.parsed.toString().replace(">", ">\n").split("\n"))
        //   Log.d(TAG, "Parsed: " + s);
        throw new SVGParseException(e);
    }
}

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;
                }/*  w  w  w . ja  v  a 2 s . c  om*/
                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);
}