Coder Social home page Coder Social logo

xamsvg's Introduction

XamSvg

XamSvg is a library to parse and render SVG images with Xamarin.Android

It's mainly a rough port of AndEngine SVG plugin with added sugar for normal Android development.

screenshot

Usage

Preparing your SVG

First and foremost, this library is not meant to be a full blown SVG parser. Rather the idea is to support a small subset of the format to allow scalable shapes to be used on Android. This means that path, shapes, color and gradients are supported but basically nothing else (e.g. pretty much no SVG effects).

It's also possible that some higher-level SVG constructs used by graphic softwares don't meddle well with the parser. In that case, you should try to export the image in a "lower-compatibility" or "simplified" mode. The excellent (and free) Inkscape SVG editor for instance supports saving pictures in a "Optimized SVG" format that is more suitable to be consummed by the library.

Basic

The most straightforward way to use the library is through SvgFactory.

// Get a bitmap from resources (also raw SVG string, TextReader and Stream)
var bmp = SvgFactory.GetBitmap (Activity.Resources, Resource.Raw.ic_svg, 48, 48);

// Get a drawable that will automatically scale with the drawer bounds
var drawable = SvgFactory.GetDrawable (Activity.Resources, Resource.Raw.ic_svg);

If you are using the Drawable factory method, know that you should specify some bounds first. If you are using an ImageView, this can be simply done by specifying it like this in XML:

<ImageView
	android:layout_width="14dp"
	android:layout_height="9dp"
	android:scaleType="fitXY" />

Note that we also use the fitXY value for scale type to avoid any canvas transformation.

In that case, the drawable will automatically generate a bitmap representation of your SVG at the right size and density.

Color mapper

Every factory method supports an optional color mapper parameter that let you dynamically change the color of the SVG being parsed. This is useful if you want to reuse the same SVG multiple time with a different tint color.

You can create a mapper by implementing the ISvgColorMapper interface or directly create one with a Func<Color, Color> using the SvgColorMapperFactory.FromFunc method.

Low-level, directly using SvgParser

If you want to have directly access to the parsed SVG representation, you can use the methods of the SvgParser class which return a Svg instance.

That instance has a Picture property returning (surprise) a Picture object that contains all the drawing calls necessary to render the SVG.

Bigger example

Moyeu uses this library for pretty much all of its icon. Since it's also available under an open-source license, it's a good way to see how the library can be used.

License

This work is placed under the Apache 2.0 license.

xamsvg's People

Contributors

garuma avatar cheesebaron avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.