svg-android

repository·master·Indexed 20 days ago

https://github.com/japgolly/svg-android

A library for parsing and rendering SVG files on Android. It allows loading SVGs from resources or assets using SVGBuilder and rendering them either directly to a Canvas via a Picture object or as a standard Android Drawable.

Tokens
862
Snippets
3
Records
3
Agent score
19%

What's inside svg-android

  1. Install svg-android via Maven

    master

    To include this library in your Android project, add the following dependency to your pom.xml file. Note that this project is currently unmaintained and discontinued.

    <dependency>
      <groupId>com.github.japgolly.android</groupId>
      <artifactId>svg-android</artifactId>
    	<version>2.0.6</version>
    </dependency>
  2. Draw an SVG onto a Canvas or as a Drawable

    master

    Once you have an SVG instance, you can render it in two primary ways:

    1. Directly onto a Canvas: Use svg.getPicture() to get a Picture object, which can then be drawn using canvas.drawPicture().
    2. As an Android Drawable: Use svg.createDrawable() to create a Drawable instance. This can be drawn onto a canvas via drawable.draw(canvas) or set directly on an ImageView using imageView.setImageDrawable(drawable).
    // Draw onto a canvas
    canvas.drawPicture(svg.getPicture());
    
    // Turn into a drawable
    Drawable drawable = svg.createDrawable();
    // drawable.draw(canvas);
    // imageView.setImageDrawable(drawable);
  3. Load and parse an SVG using SVGBuilder

    master

    To parse an SVG file, use the SVGBuilder class. You should first store your SVG files in the res/raw or assets directory of your Android project.

    SVGBuilder provides several methods for loading and configuring the SVG:

    • readFromResource(Resources resources, int resId): Loads an SVG from res/raw.
    • readFromAsset(AssetManager assets, String fileName): Loads an SVG from the assets folder.
    • setWhiteMode(boolean): If true, draws fills in white and does not draw strokes.
    • setColorSwap(int oldColor, int newColor): Swaps a specific color in the SVG.
    • setColorFilter(ColorFilter filter): Applies a color filter to the entire SVG.
    • setStrokeColorFilter(ColorFilter filter): Applies a color filter specifically to the strokes.
    • setFillColorFilter(ColorFilter filter): Applies a color filter specifically to the fills.

    After configuring the builder, call .build() to obtain an SVG instance.

    // Load and parse a SVG
    SVG svg = new SVGBuilder()
                .readFromResource(getResources(), R.raw.someSvgResource) // if svg in res/raw
                .readFromAsset(getAssets(), "somePicture.svg")           // if svg in assets
                // .setWhiteMode(true) // draw fills in white, doesn't draw strokes
                // .setColorSwap(0xFF008800, 0xFF33AAFF) // swap a single colour
                // .setColorFilter(filter) // run through a colour filter
                // .set[Stroke|Fill]ColorFilter(filter) // apply a colour filter to only the stroke or fill
                .build();