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:
dependencies {
compile 'com.pixplicity.sharp:library:[VERSION_HERE]'
}
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);
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 aSharpDrawable
, which is a subclass ofPictureDrawable
that respects theSharpPicture
boundaries.getDrawable(View, DrawableCallback)
does the same, but on a background thread with callbacks ofDrawableCallback
;getSharpPicture()
generates aSharpPicture
, a wrapper containing aPicture
and the SVG bounds and limits;getSharpPicture(PictureCallback)
does the same, but on a background thread with callbacks ofPictureCallback
;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
- Text size and position isn't accurate. It's recommended to convert all text to paths in order for it to appear pixel-perfect.
- 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 usingloadAsset()
) instead.
License
Licensed under the Apache license 2.0.