Giter Site home page Giter Site logo

sharp's Introduction

Sharp

Sharp is a Scalable Vector Graphics (SVG) implementation for Android. It facilitates loading vector graphics as SharpDrawables, and can effectively be used wherever a conventional image would be displayed, whether it be as a background, ImageView source, inside a StateListDrawable or used as composites in a TextView.

Forked from:
https://github.com/pents90/svg-android

Merged changes from forks:
https://github.com/b2renger/svg-android
https://github.com/mindon/svg-android
https://github.com/josefpavlik/svg-android

Download

Download the latest AAR or grab from Bintray using Gradle:

repositories {
    maven { url "http://dl.bintray.com/pixplicity/android" }
}
dependencies {
    compile 'com.pixplicity.sharp:library:[VERSION_HERE]@aar'
}

Download

Important: Specify the latest version from Bintray as [VERSION_HERE].

Sample

A sample is included in this repository.

It's easy to load an SVG:

Sharp.loadResource(getResources(), R.raw.cartman)
     .into(mImageView);

Sample screenshot

Sample screenshot

Sample screenshot

SVGs can be loaded from various sources:

  • loadAsset(AssetManager, String) loads SVG data from an Android application asset;
  • loadResource(Resources, int) loads SVG data from an Android application resource;
  • loadString(String) loads SVG data directly from a String;
  • loadInputStream(InputStream) loads SVG data from an InputStream (but it's your responsibility to close it afterwards);
  • loadFile(File) loads SVG data from a File, internally opening and closing a FileInputStream to do so.
  • loadPath(String) loads SVG data directly from a String (but uses a lot of memory doing so).

Sharp facilitates the application of the resulting drawable as well, through the following methods:

  • into(View) takes care of loading the SVG into the source of the ImageView, or setting the background if the view is not an ImageView;
  • getDrawable(View) generates a SharpDrawable, which is a subclass of PictureDrawable that respects the SharpPicture boundaries.
  • getDrawable(View, DrawableCallback) does the same, but on a background thread with callbacks of DrawableCallback;
  • getSharpPicture() generates a SharpPicture, a wrapper containing a Picture and the SVG bounds and limits;
  • getSharpPicture(PictureCallback) does the same, but on a background thread with callbacks of PictureCallback;
  • withAssets(AssetManager) provides access to your application's assets, allowing Sharp to read typefaces;

It's recommended to use into(View) or getDrawable(View), as the View parameter takes care of setting the view's layer type to View.LAYER_TYPE_SOFTWARE.

Typefaces

By loading an SVG from the assets directory using loadAsset(AssetManager, String), or by loading it through a different mechanism and providing access to your application's assets using withAssets(AssetManager), Sharp can read typeface files automatically from the assets directory.

Sharp expects the typeface to be present as:

/assets/fonts/[typeface name].ttf

If your font does not appear, check Logcat for any insightful error messages.

Why isn't my SVG appearing?

If you're setting your view's drawable manually, instead of using into() or intoBackground(), be sure to set the view's layer type to View.LAYER_TYPE_SOFTWARE.

Why no hardware acceleration?

Excellent question! Aside from the fact that PictureDrawable doesn't render correctly, paths do not efficiently render in hardware acceleration. Even if it worked, it would have poor performance. Read this excellent discussion about why this is, if you're interested.

You don't need to disable hardware acceleration on your entire application. Only individual views need to have the layer type changed, and providing your view into SharpPicture.getDrawable(View) takes care of this for you.

Known issues

  1. Text size and position isn't accurate. It's recommended to convert all text to paths in order for it to appear pixel-perfect.
  2. Android's Gradle plugin 2.0.0 (in the preview channel as of January 2016) no longer allows you to place SVG files in /res/drawable. We recommend placing them in /res/raw (or /assets and using loadAsset()) instead.

License

Licensed under the Apache license 2.0.

sharp's People

Contributors

pflammertsma avatar josefpavlik avatar mindon avatar pents90 avatar mlagerberg avatar

Watchers

James Cloos avatar dev.with 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.