How do I render an SVG in an Android app? - android

After finding Display SVG Image in image view in android on Google and going to Having issue on Real Device using vector image in android. SVG-android, I find that "You need a 3rd-party JAR to do it" is a bit of overkill. Is there any way to render an SVG without using 3rd-party libraries?

I managed to display an SVG using a WebView, since that is more than capable of rendering an SVG:
webView.loadUrl("file:///android_res/drawable/file.svg"); // point it to the SVG
webView.setBackgroundColor(0x00000000); // set the background to transparent
This makes it render much like a transparent PNG would in an ImageView. The only caveat is that the SVG must use the viewBox attribute and not use the height or width attributes, to ensure it resizes properly.

Using custom fonts actually works really well in Android.
You can use the free icomoon web application to convert SVGs to custom .ttf font characters.

If you are using it for imagePicker sort of feature then,
webView.loadURl(selectedFileLocation.toString)
else if it is to display a .svg image from web,
webView.loadURl(/*url for the .svg file*/)

Related

When i use .svg file "ERROR #<image> is not supported" in Android Studio

I want to create a Splash screen for my android application for this purpose i create ".svg" file by using three different ".png" images using Adobe XD. bust when i import ".svg" file through vector Asset it only show background of ".svg" image it does not show other images which i used to create this file for splash screen. I also used this http://inloop.github.io/svg2android/ to get proper ".xml" file and past it in drawable folder but still no change. I am attaching images of my error this is where error is shown
when i click on error foe more detail i I found this error
I want to use my app logo for splash screen "Please Help" me an tell me how to use logo on splash screen i just started android development.
Thankyou.
The Android Studio SVG importer only supports a subset of SVG. Remember that it is converting the SVG to a VectorDrawable. So only things that work in a VectorDrawable can be imported from the SVG. Basically that means you must stick to just the vector shapes - rectangles, ellipses, paths etc
See: Which SVG elements are supported by Android studio and which are not?
If your SVG only contains PNG images, then there is no point in using an SVG anyway. Just import your PNGs to your project and use an ImageView to display them.
If you really need to display an SVG. Then use a library that properly supports SVGs. Such as my one: AndroidSVG.

Android Studio does not import all parts of an SVG Vector

I want to import some vector graphics from illustrator for the design of my android studio app. If I export my vector graphics as SVG, this works wonderfully. All parts of my project are successfully exported and the result is satisfactory (image 1). However, when I create this SVG file as a new Vector Asset in Android Studio, one of my blue circles is suddenly missing (see image 2). What can be the reasons why Android studio does not import all parts of my SVG? Or could the reason be a wrong export from illustrator?
Edit: Added Picture 3 to show what happens if I use a online converter instead. The output is even worse.
Any Help is appreciated!
Picture 1:
Picture 2:
Image 3...Result when I use a online converter to get a xml vector(also wrong)*
The version of the SVG that is displayed in the converter window is rendered by a built-in SVG renderer in Android Studio. It's a preview of the SVG. It does not necessarily reflect what the output of the converter will be.
So the bug you are seeing is with the built-in SVG renderer.
VectorDrawables don't really support gradient fills. At least the converter doesn't support them. So even if the displayed SVG were perfect, the generated VectorDrawable won't include the gradient circles anyway.
So you have a few alternative approaches:
Change your circles to solid fill and then convert to VectorDrawables.
Like #1, but add gradients by using predefined gradient definitions.
Use an actual SVG rendering library (like AndroidSVG) in your app.
Switch to using a bitmap background (ie PNG)
Draw the background yourself using Canvas methods.
Personally, I would go with #3.
Android Studio doesn't convert complex graphics into vector drawable. It only converts flat icons. You have to use a png here dude !!

How to fill svg shape when i touch on svg image without loss quality of svg

This image is SVG image which is loaded into SVGImageView. I can't fill color into SVG with onTouch listener. I am tired to fill color into particular shape or area. Help me. Thank you.
https://i.stack.imgur.com/96TM5.png
And fill area with different color like this :
https://i.stack.imgur.com/t9zXG.jpg
AFAIK at the moment none of the SVG libraries for Android support interaction. I am the author of AndroidSVG and it is a high priority on my list of things to implement.
If you need it now, however, I think you will need to resort to displaying the SVG in a WebView and using Javascript events to determine what is clicked on.

SVG image not displaying using the standard code

I am using the standard code stated in the example of the library of https://code.google.com/p/svg-android/wiki/Tutorial, here is my OnCreate method :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
ImageView imageView = (ImageView)findViewById(R.id.imageViewTest);
// Set the background color to white
imageView.setBackgroundColor(Color.GRAY);
// Parse the SVG file from the resource
SVG svg = SVGParser.getSVGFromResource(getResources(), R.drawable.android);
//Get a drawable from the parsed SVG and set it as the drawable for the ImageView
imageView.setImageDrawable(svg.createPictureDrawable());
}
I am not able to add the layout code here, so sharing it in this doc: https://docs.google.com/document/d/1fbi3B_hAYUh_C2IwPfInvZ-BG2bgsa4pZoJKj8NBT9o/edit?usp=sharing
It does not give any error but it also does not display the image.
I was earlier getting doubts that the image is incorrect, then I used the one in the same example.
Yet it is not displaying the image nor giving any error.
Please suggest how to debug further.
On newer devices have hardware rendering turned on by default so you need to explicitly turn on software rendering.
use this after your code:
imageView.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
Your problem is almost certainly hardware acceleration. You may need to set the View LayerType to software mode.
See Having issue on Real Device using vector image in android. SVG-android
If that doesn't fix it, then it may be an issue with svg-android, which can have trouble rendering correctly anything other than simple SVGs. You might have better luck with my library AndroidSVG.
The best practice for SVG on Android is going to be to use a tool to convert your SVG to PNG at the size(s) you're interested in. Existing SVG support for Android is not comprehensive of what you're likely to find in an SVG file, and even if it were, the support is not built into the OS so using them directly is out of source .
If the library you're working with can process the SVGs you have well, you can make it work for every icon but not through the standard Android API; you'll need to create a custom view. Around months ago I used the library you linked and at that time it had trouble with many SVGs I had created in Inkscape or downloaded from various places. Perhaps its support has improved since then, but I recommend testing it with the exact SVGs you plan to use before you write a lot of custom code for it.

Is it possible to display svgz on Android?

is there a simple way to display SVGZ images within an Android app. I know that there are a few libraries to display SVG images. But they do not support SVGZ as far as I know.
In case that there is no library that can display SVGZ images, is it possible to unzip the file with java.util.zip?
Yes it should be possible to extract the svg file and visualize it with a small lib called svg-android.
Small Example:
GZIPInputStream is=/*...*/;
PictureDrawable img = SVGParser.getSVGFromInputStream(is, 0, 0)
.createPictureDrawable();
Try this fork of svg-android.
It detects svgz automatically, so you can do directly
SVGParser.getSVGFromResource(getResources(), R.raw.mysvgz);
and it is more compatible with the svg definition. In other words, the original version of svg-android can not view many svgs due to lacks of some features.

Categories

Resources