com.caverock.androidsvg.SVG Java Examples

The following examples show how to use com.caverock.androidsvg.SVG. 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: SvgBitmapDrawableTranscoder.java    From SvgGlidePlugins with Apache License 2.0 6 votes vote down vote up
private void prepareSvg(@NonNull Resource<SVG> toTranscode, @Nullable Options options) {
    if (!(toTranscode instanceof SvgResource)) {
        return;
    }

    DownsampleStrategy strategy =
            options == null ? null : options.get(DownsampleStrategy.OPTION);
    if (strategy != null) {
        float scaleFactor = strategy.getScaleFactor(
                Math.round(toTranscode.get().getDocumentWidth()),
                Math.round(toTranscode.get().getDocumentHeight()),
                ((SvgResource) toTranscode).getWidth(),
                ((SvgResource) toTranscode).getHeight()
        );
        SvgUtils.scaleDocumentSize(toTranscode.get(), scaleFactor);
    }
}
 
Example #2
Source File: SvgMediaDecoder.java    From Markwon with Apache License 2.0 6 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 float w = svg.getDocumentWidth();
    final float h = svg.getDocumentHeight();
    final float density = resources.getDisplayMetrics().density;

    final int width = (int) (w * density + .5F);
    final int height = (int) (h * density + .5F);

    final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
    final Canvas canvas = new Canvas(bitmap);
    canvas.scale(density, density);
    svg.renderToCanvas(canvas);

    return new BitmapDrawable(resources, bitmap);
}
 
Example #3
Source File: GroupConversation.java    From NaviBee with GNU General Public License v3.0 6 votes vote down vote up
private static Bitmap imageFromString(String svgAsString) throws SVGParseException {

        SVG svg = SVG.getFromString(svgAsString);

        // Create a bitmap and canvas to draw onto
        float   svgWidth = (svg.getDocumentWidth() != -1) ? svg.getDocumentWidth() : 500f;
        float   svgHeight = (svg.getDocumentHeight() != -1) ? svg.getDocumentHeight() : 500f;

        Bitmap  newBM = Bitmap.createBitmap(Math.round(svgWidth),
                Math.round(svgHeight),
                Bitmap.Config.ARGB_8888);
        Canvas bmcanvas = new Canvas(newBM);

        // Clear background to white if you want
        bmcanvas.drawRGB(255, 255, 255);

        // Render our document onto our canvas
        svg.renderToCanvas(bmcanvas);

        return newBM;
    }
 
Example #4
Source File: SvgUtils.java    From SvgGlidePlugins with Apache License 2.0 6 votes vote down vote up
public static void fix(@NonNull SVG svg) throws IOException {
    RectF viewBox = svg.getDocumentViewBox();
    float docWidth = svg.getDocumentWidth();
    float docHeight = svg.getDocumentHeight();

    if (viewBox == null) {
        if (docWidth > 0 && docHeight > 0) {
            svg.setDocumentViewBox(0F, 0F, docWidth, docHeight);
        } else {
            throw new IOException("SVG must have specify 'width' & 'height' tags or 'viewbox'");
        }

    } else if (docWidth <= 0 && docHeight <= 0) {
        svg.setDocumentWidth(viewBox.width());
        svg.setDocumentHeight(viewBox.height());

    } else if (docWidth <= 0) {
        svg.setDocumentWidth(aspectRation(viewBox) * docHeight);

    } else if (docHeight <= 0) {
        svg.setDocumentHeight(docWidth / aspectRation(viewBox));
    }
}
 
Example #5
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 #6
Source File: CustomImageView.java    From microMathematics with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Parcelable interface: procedure writes the formula state
 */
@Override
@SuppressLint("MissingSuperCall")
public Parcelable onSaveInstanceState()
{
    Bundle bundle = new Bundle();
    bundle.putString(STATE_IMAGE_TYPE, imageType.toString());
    if (externalUri != null)
    {
        bundle.putString(STATE_IMAGE_URI, externalUri.toString());
    }
    else if (imageType == ImageType.BITMAP && bitmap != null)
    {
        bundle.putString(STATE_IMAGE_BITMAP, getEncodedImage(bitmap));
    }
    else if (imageType == ImageType.SVG && svgData != null)
    {
        bundle.putString(STATE_IMAGE_SVG, svgData);
    }
    return bundle;
}
 
Example #7
Source File: ImageZoomPresenter.java    From incubator-taverna-mobile with Apache License 2.0 6 votes vote down vote up
@Override
public void attachView(ImageZoomMvpView mvpView) {

    super.attachView(mvpView);

    requestBuilder = Glide.with(getMvpView().getAppContext())
            .using(Glide.buildStreamModelLoader(Uri.class,
                    getMvpView().getAppContext()), InputStream.class)
            .from(Uri.class)
            .as(SVG.class)
            .transcode(new SvgDrawableTranscoder(), PictureDrawable.class)
            .sourceEncoder(new StreamEncoder())
            .cacheDecoder(new FileToStreamDecoder<SVG>(new SvgDecoder()))
            .decoder(new SvgDecoder())
            .placeholder(R.drawable.placeholder)
            .error(R.drawable.placeholder)
            .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
            .animate(android.R.anim.fade_in);

}
 
Example #8
Source File: CustomImageView.java    From microMathematics with GNU General Public License v3.0 6 votes vote down vote up
private void setSvg(String svgData)
{
    svg = null;
    try
    {
        svg = SVG.getFromString(svgData);
        originalWidth = (int) svg.getDocumentWidth();
        originalHeight = (int) svg.getDocumentHeight();
        svg.setDocumentWidth("100%");
        svg.setDocumentHeight("100%");
        svg.setDocumentViewBox(0, 0, originalWidth, originalHeight);
    }
    catch (SVGParseException e)
    {
        // nothing to do
    }
    if (svg != null)
    {
        this.svgData = svgData;
    }
    imageType = this.svg == null ? ImageType.NONE : ImageType.SVG;
}
 
Example #9
Source File: VectorDrawable.java    From Carbon with Apache License 2.0 6 votes vote down vote up
public VectorDrawable(Resources res, int resId) {
    if (resId == 0)
        return;
    try {
        SVG svg = cache.get(resId);
        if (svg == null) {
            svg = SVG.getFromResource(res, resId);
            cache.put(resId, svg);
        }
        float density = res.getDisplayMetrics().density;

        float width = svg.getDocumentViewBox().width();
        float height = svg.getDocumentViewBox().height();

        int intWidth = (int) (width * density);
        int intHeight = (int) (height * density);
        state = new VectorDrawableState(svg, intWidth, intHeight);
        setBounds(0, 0, state.intWidth, state.intHeight);
    } catch (SVGParseException e) {

    }
}
 
Example #10
Source File: UIHelpers.java    From proofmode with GNU General Public License v3.0 6 votes vote down vote up
public static void populateContainerWithSVG(View rootView, int offsetX, int idSVG, int idContainer) {
	try {
		SVG svg = SVG.getFromResource(rootView.getContext(), idSVG);
		if (offsetX != 0) {
			RectF viewBox = svg.getDocumentViewBox();
			viewBox.offset(offsetX, 0);
			svg.setDocumentViewBox(viewBox.left, viewBox.top, viewBox.width(), viewBox.height());
		}
		SVGImageView svgImageView = new SVGImageView(rootView.getContext());
		svgImageView.setFocusable(false);
		svgImageView.setFocusableInTouchMode(false);
		svgImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
		svgImageView.setSVG(svg);
		svgImageView.setLayoutParams(new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));
		ViewGroup layout = (ViewGroup) rootView.findViewById(idContainer);
		layout.addView(svgImageView);
	} catch (SVGParseException e) {
		e.printStackTrace();
	}
}
 
Example #11
Source File: SvgUtils.java    From ACDD with MIT License 5 votes vote down vote up
/**
 * Loading the svg from the resources.
 *
 * @param context     Context object to get the resources.
 * @param svgResource int resource id of the svg.
 */
public void load(Context context, int svgResource) {
    if (mSvg != null) return;
    try {
        mSvg = SVG.getFromResource(context, svgResource);
        mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
    } catch (SVGParseException e) {
        Log.e(LOG_TAG, "Could not load specified SVG resource", e);
    }
}
 
Example #12
Source File: SvgBitmapDrawableTranscoder.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<BitmapDrawable> transcode(
        @NonNull Resource<SVG> toTranscode, @Nullable Options options) {
    prepareSvg(toTranscode, options);
    Bitmap bitmap = SvgUtils.toBitmap(toTranscode.get(), mBitmapProvider, getDecodeFormat(options));
    return LazyBitmapDrawableResource.obtain(mResources, new BitmapResource(bitmap, mBitmapPool));
}
 
Example #13
Source File: SvgDecoder.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
public Resource<SVG> decode(InputStream source, int width, int height) throws IOException {
    try {
        SVG svg = SVG.getFromInputStream(source);
        return new SimpleResource<SVG>(svg);
    } catch (SVGParseException ex) {
        throw new IOException("Cannot load SVG from stream", ex);
    }
}
 
Example #14
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 #15
Source File: CustomImageView.java    From microMathematics with GNU General Public License v3.0 5 votes vote down vote up
/*********************************************************
 * Painting
 *********************************************************/

@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas c)
{
    try
    {
        rect.set(getPaddingLeft(), getPaddingTop(), this.getRight() - this.getLeft() - getPaddingRight() - 1,
                this.getBottom() - this.getTop() - getPaddingBottom() - 1);
        paint.setColor(getCurrentTextColor());
        paint.setStrokeWidth(strokeWidth);
        if (imageType == ImageType.SVG && svg != null)
        {
            final int width = (int) rect.width();
            final int height = (int) rect.height();
            bitmap = ViewUtils.pictureToBitmap(svg.renderToPicture(width, height), width, height);
            paint.setColorFilter(colorFilter);
            c.drawBitmap(bitmap, null, rect, paint);
        }
        else if (imageType == ImageType.BITMAP && bitmap != null)
        {
            paint.setColorFilter(colorFilter);
            c.drawBitmap(bitmap, null, rect, paint);
        }
        else
        {
            // do not set color filter for text image
            super.onDraw(c);
        }
    }
    catch (OutOfMemoryError | Exception ex)
    {
        String error = getContext().getResources().getString(R.string.error_out_of_memory);
        Toast.makeText(getContext(), error, Toast.LENGTH_LONG).show();
    }
}
 
Example #16
Source File: NativeOpenStreetMapController.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
private static float getBestGuessWidth(SVG.Svg svg) {
  if (svg.width != null) {
    return svg.width.floatValue();
  } else if (svg.viewBox != null) {
    return svg.viewBox.width;
  } else {
    return ComponentConstants.MARKER_PREFERRED_WIDTH;
  }
}
 
Example #17
Source File: NativeOpenStreetMapController.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
private static float getBestGuessHeight(SVG.Svg svg) {
  if (svg.height != null) {
    return svg.height.floatValue();
  } else if (svg.viewBox != null) {
    return svg.viewBox.height;
  } else {
    return ComponentConstants.MARKER_PREFERRED_HEIGHT;
  }
}
 
Example #18
Source File: SVGMediaMetadataRetriever.java    From MediaMetadataRetrieverCompat with MIT License 5 votes vote down vote up
@Override
public void setDataSource(@NonNull DataSource source) throws IOException {
    try {
        mSVG = SVG.getFromInputStream(source.toStream());
    } catch (SVGParseException e) {
        e.printStackTrace();
    }
}
 
Example #19
Source File: SvgDecoder.java    From incubator-taverna-mobile with Apache License 2.0 5 votes vote down vote up
public Resource<SVG> decode(InputStream source, int width, int height) throws IOException {
    try {
        SVG svg = SVG.getFromInputStream(source);
        return new SimpleResource<SVG>(svg);
    } catch (SVGParseException ex) {
        throw new IOException("Cannot load SVG from stream", ex);
    }
}
 
Example #20
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 #21
Source File: SvgUtils.java    From android-pathview with Apache License 2.0 5 votes vote down vote up
/**
 * Loading the svg from the resources.
 *
 * @param context     Context object to get the resources.
 * @param svgResource int resource id of the svg.
 */
public void load(Context context, int svgResource) {
    if (mSvg != null) 
        return;
    try {
        mSvg = SVG.getFromResource(context, svgResource);
        mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
    } catch (SVGParseException e) {
        Log.e(LOG_TAG, "Could not load specified SVG resource", e);
    }
}
 
Example #22
Source File: SvgDecoderExample.java    From fresco with MIT License 5 votes vote down vote up
@Override
public CloseableImage decode(
    EncodedImage encodedImage,
    int length,
    QualityInfo qualityInfo,
    ImageDecodeOptions options) {
  try {
    SVG svg = SVG.getFromInputStream(encodedImage.getInputStream());
    return new CloseableSvgImage(svg);
  } catch (SVGParseException e) {
    e.printStackTrace();
  }
  return null;
}
 
Example #23
Source File: SvgHelper.java    From google-io-2014-compat with Apache License 2.0 5 votes vote down vote up
public void load(Context context, int svgResource) {
    if (mSvg != null) return;
    try {
        mSvg = SVG.getFromResource(context, svgResource);
        mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
    } catch (SVGParseException e) {
        Log.e(LOG_TAG, "Could not load specified SVG resource", e);
    }
}
 
Example #24
Source File: SvgHelper.java    From Android-Anim-Playground with MIT License 5 votes vote down vote up
public void load(Context context, int svgResource) {
    if (mSvg != null) return;
    try {
        mSvg = SVG.getFromResource(context, svgResource);
        mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
    } catch (SVGParseException e) {
        Log.e(LOG_TAG, "Could not load specified SVG resource", e);
    }
}
 
Example #25
Source File: SvgHelper.java    From google-io-2014 with Apache License 2.0 5 votes vote down vote up
public void load(Context context, int svgResource) {
    if (mSvg != null) return;
    try {
        mSvg = SVG.getFromResource(context, svgResource);
        mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
    } catch (SVGParseException e) {
        Log.e(LOG_TAG, "Could not load specified SVG resource", e);
    }
}
 
Example #26
Source File: SvgHelper.java    From road-trip with Apache License 2.0 5 votes vote down vote up
public void load(Context context, int svgResource) {
    if (mSvg != null) return;
    try {
        mSvg = SVG.getFromResource(context, svgResource);
        mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
    } catch (SVGParseException e) {
        Log.e(LOG_TAG, "Could not load specified SVG resource", e);
    }
}
 
Example #27
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 #28
Source File: SvgUtils.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
public static SVG getSvg(@NonNull FileDescriptor descriptor)
        throws SVGParseException, IOException {

    try (InputStream is = new BufferedInputStream(new FileInputStream(descriptor))) {
        return SVG.getFromInputStream(is);
    }
}
 
Example #29
Source File: SvgUtils.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
public static void scaleDocumentSize(
        @NonNull SVG svg,
        @FloatRange(from = 0, fromInclusive = false) float scale
) {
    svg.setDocumentWidth(svg.getDocumentWidth() * scale);
    svg.setDocumentHeight(svg.getDocumentHeight() * scale);
}
 
Example #30
Source File: SvgUtils.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
@NonNull
public static Bitmap toBitmap(
        @NonNull SVG svg,
        @NonNull BitmapProvider provider,
        @NonNull Bitmap.Config config
) {
    int outImageWidth = Math.round(svg.getDocumentWidth());
    int outImageHeight = Math.round(svg.getDocumentHeight());
    Bitmap bitmap = provider.get(outImageWidth, outImageHeight, config);
    Canvas canvas = new Canvas(bitmap);
    svg.renderToCanvas(canvas);
    return bitmap;
}