Java Code Examples for com.caverock.androidsvg.SVG#renderToPicture()

The following examples show how to use com.caverock.androidsvg.SVG#renderToPicture() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AndroidSvgBitmap.java    From trekarta with GNU General Public License v3.0 6 votes vote down vote up
public static android.graphics.Bitmap getResourceBitmap(InputStream inputStream, float scaleFactor, float defaultSize, int width, int height, int percent, int color) throws IOException {
    try {
        SVG svg = SVG.getFromInputStream(inputStream);
        Picture picture;
        if (color != 0) {
            RenderOptions renderOpts = RenderOptions.create().css("* { fill: #" + String.format("%06x", color & 0x00ffffff) + "; }");
            picture = svg.renderToPicture(renderOpts);
        } else {
            picture = svg.renderToPicture();
        }

        double scale = scaleFactor / Math.sqrt((picture.getHeight() * picture.getWidth()) / defaultSize);

        float[] bmpSize = GraphicUtils.imageSize(picture.getWidth(), picture.getHeight(), (float) scale, width, height, percent);

        android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap((int) Math.ceil(bmpSize[0]),
                (int) Math.ceil(bmpSize[1]), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawPicture(picture, new RectF(0, 0, bmpSize[0], bmpSize[1]));

        return bitmap;
    } catch (Exception e) {
        throw new IOException(e);
    }
}
 
Example 2
Source File: SvgDrawableTranscoder.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Resource<PictureDrawable> transcode(Resource<SVG> toTranscode) {
    SVG svg = toTranscode.get();
    Picture picture = svg.renderToPicture();
    PictureDrawable drawable = new PictureDrawable(picture);
    return new SimpleResource<PictureDrawable>(drawable);
}
 
Example 3
Source File: SvgDrawableTranscoder.java    From GlideToVectorYou with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public Resource<PictureDrawable> transcode(@NonNull Resource<SVG> toTranscode,
                                           @NonNull Options options) {
  SVG svg = toTranscode.get();
  Picture picture = svg.renderToPicture();
  PictureDrawable drawable = new PictureDrawable(picture);
  return new SimpleResource<>(drawable);
}
 
Example 4
Source File: SvgPictureMediaDecoder.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public Drawable decode(@Nullable String contentType, @NonNull InputStream inputStream) {

    final SVG svg;
    try {
        svg = SVG.getFromInputStream(inputStream);
    } catch (SVGParseException e) {
        throw new IllegalStateException("Exception decoding SVG", e);
    }

    final Picture picture = svg.renderToPicture();
    return new PictureDrawable(picture);
}
 
Example 5
Source File: SvgDrawableTranscoder.java    From incubator-taverna-mobile with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<PictureDrawable> transcode(Resource<SVG> toTranscode) {
    SVG svg = toTranscode.get();
    Picture picture = svg.renderToPicture();
    PictureDrawable drawable = new PictureDrawable(picture);
    return new SimpleResource<PictureDrawable>(drawable);
}
 
Example 6
Source File: NativeOpenStreetMapController.java    From appinventor-extensions with Apache License 2.0 4 votes vote down vote up
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;
      path.baseStyle.specifiedFlags = 0x3d;
      if (path.style != null) {
        if ((path.style.specifiedFlags & SPECIFIED_FILL) == 0) {
          path.style.fill = new SVG.Colour(fillPaint.getColor());
          path.style.specifiedFlags |= SPECIFIED_FILL;
        }
        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);
}