Convert svg to VectorDrawable xml at compile time? - android

We convert SVG into the simpler VectorDrawable format all the time for use in Android, but soon it becomes a maintenance issue and we never know if a VectorDrawable is up to date.
One way to do that is to keep the SVG in the source repo, but gitignore the VectorDrawable XML files; then to write a Makefile to generate the needed XML files. But given gradle is doing the same job as Makefile, there might be a better gradle way to do so?
It is desirable if we can skip the creation of VectorDrawable XML in source (i.e. build/intermediate would be great).
I am not looking for libraries to render SVG files at runtime. Just something to streamline the SVG-update -> Drawable XML update process, so developers can focus on coding instead of managing resources.Thanks!

Related

Android Studio Vector asset always could not generate a preview

I want to use vector asset in android studio to generate svg xml file,
I convert my png to svg in this web site
but when I use this svg file in vector asset to generate xml file, I get following error message
It display "could not generate a preview"
How can I fix it?
Most of the complaints on this topic are due to people trying to convert SVGs that use features of the SVG standard that are not supported by VectorDrawables. And the SVG to VectorDrawable converter in AndroidStudio only supports those limited features. It doesn't really give much feedback when it encounters things it doesn't support.
Some of the online converters try a bit harder. You might have better success with them.
Otherwise you may have to simplify your SVGs to get them to work.
If you need to display SVGs images that are more complicated than VectorDrawables can handle, then you can always consider using an SVG rendering library for Android. IMHO the best one is AndroidSVG (disclaimer I wrote that one).
There are some cases when your actual .svg file name contains some capital letters. Just rename them in all small letters and try to upload it.
For instance:
If actual .svg image name is mySavedimage.svg {here 'S' is capital which is against norms of android assets}. SO if you rename that file to my_saved_image.svg then this will work as a charm.
hope it helps.
I've never had any luck with those convert sites. You're better off using photo tools to create an SVG. Make sure to follow the SVG guidelines from Android listed here
The error is in the converter. The SVG file might not be proper.
You need to use photo editor tools like Photoshop or Gimp.

Import, edit and Export saved SVG files

I am having a requirement where I have to generate a single svg file from various svg file like shapes into the canvas in android platform like making a image. And above that after saving that svg file I can also re-import that svg file and can undo/redo the changes done previously as well as making adding new stuff and saving the new svg file.
I am not much aware regarding importing and reusing the svg file after once it is saved and redo/undo or edit the previous changes step by step according to the layers in svg file and additionally making new changes into it.
I had R&D in figuring out the way but could not find any helpful solution. It would be great help if anyone can suggest me convenient ways to do that so.
Thank you.

Creating a VectorDrawable from an SVG file at runtime

I want to create an VectorDrawable from a remote SVG file (ie a URL), within my app (not in Android Studio). After a bit of searching, I have yet to find a way to do this programmatically, is there even a way to do this?
Any help would be appreciated.
First yo need to download the resource like a file or InputStream class type.
After that, you must transform to a graphic class. To do that I found this library: Library tutorial
Good Luck!

How to make an application's vector drawable override a library's vector drawable?

I have a library project and an application project. Beside other things the libary project contains some png and some vector drawables. Now I can easily overwrite a png drawable in the application project by giving it the same name and it will be displayed correctly. This does not work with vector drawables, though: The app always shows the vector drawables defined in the library, both on Android 4 and 5. The app would never show the application project's vector drawables.
Google claims an application's resources always have priority over a library's resources:
Since the tools merge the resources of a library module with those of a dependent application module, a given resource ID might be defined in both modules. In this case, the tools select the resource from the application, or the library with highest priority, and discard the other resource. As you develop your applications, be aware that common resource IDs are likely to be defined in more than one project and will be merged, with the resource from the application or highest-priority library taking precedence.
But as I said, in case of vector drawables, for some reason, it's the other way round. Any idea what I can do to make sure the vector drawables are overridden just like normal drawables and other resources are?
UPDATE: Resolved in support library v23.2! Nothing to do now :)
When the library project is built, the VectorDrawable creates PNG files for each density and places the original VectorDrawable in drawable-anydpi-v21.
If you place your VectorDrawable in drawable-anydpi-v21 in your app, then it will override the drawable from your library project. This seems like a bug and a new issue should be created (if one doesn't already exist).
NOTE: this will not replace the generated PNG files from the library. You will need to add those to your app as well to override them.
Based on the "NOTE" in Jared Rummler's answer and since this won't fit in a comment I'll post a little tutorial here for people who have trouble finding the generated PNG files:
You can find the folders with the required files inside build/intermediates/res/merged/debug or, if you are using product flavors build/intermediates/res/merged/<flavor>/debug. Now the least difficult way of copying the PNG files to the app would be to fully copy their folders, which are:
drawable-ldpi-v4
drawable-mdpi-v4
drawable-hdpi-v4
drawable-xhdpi-v4
drawable-xxhdpi-v4
drawable-xxxhdpi-v4
As a last and tedious step you should remove all files inside you don't need, i.e. those that aren't generated from your vector drawables. This is done easiest if you are using a VCS by adding only the PNGs you need. This places all redundant files under Unversioned Files.
And there you go, together with drawable-anydpi-v21 there are now 7 additional folders just because of this stupid bug :(
UPDATE: Resolved in support library v23.2! As of today, there is finally no need to do any of the above. Just make sure to use app:srcCompat instead of android:src everywhere.

Displaying SVG files in Android

I want to create an app that will display position on some floor plan. Navigation is implementing via WiFi in certain way, I've done it and so now I have a problem of displaying floor plan.
It might be in some vector format, after surfing internet for some time I've decided that it must be svg file.
I found some solutions, but it isn't working for me!
Library svg-android
There is opportunity to display .svg files, but only "simple" files. It works fine only for file in tutorial, but not for any other .svg file (for example, some other file, that you'll create with Inkscape).
So, I decided, that I'll parse .svg file, make DOM from it, somehow get objects and attributes and draw it via OpenGL ES.
Apache Batik
At first glance, very good solution, but there is a problem. Android has some "native" apache libraries and when I try to do something with batik, it throws NoClassDefFoundError, because it's searching not in batik libraries, but in "native" libraries.
Of course, we can add source code in our project, take only batik parser for .svg files and edit it in some way, but there is a lot of work, with same success we can write our own parser.
Tiny Line
There is no trial version, but if we'll see description of how it works for svg files and android, we'll see that there is only rasterization of such files and that's all.
Is there any solution better than writing own parser?
Did anyone come across this problem?
I would suggest using #1. Don't write your own parser. It's just going to be a huge headache.
How detailed does your floor plan have to be? android-svg supports SVG fairly well. It just doesn't have great support for filters or light sources. Your SVG isn't going to have those in them (I hope).
If you don't want to do that, look into quad trees. You can render out a huge image and break that down into a quadtree like format then only load the quads you require.
I have authored an SVG libaray for android the website is
http://www.vectoroid.com
the main thing missing is SVG arc support this is currently fixed and will be in a release in the near future.
I am looking for feedback on it, as i have been working on it for about a year. So if you have any please do tell ...
I've using yet another SVG for Android; seems it is relatively new.
GPLv3, CSS2 support, fonts, texts and spans, linear and radial gradients, SVG and SVGZ, initial filtering support, images from assets, from web and inline base64-encoded images. Fastest from all I've tried.
Of course filters support might be better, but except this it works well and even displays 20-megabytes SVG files.
Successfully tested these 2 libraries:
https://github.com/BigBadaboom/androidsvg - Apache License 2.0
https://scand.com/products/svgkit-android/ - needed tweaking to compile with modern SDK (suppress warnings, update gradle file); LGPL / commercial
androidsvg looks better so far.

Categories

Resources