converted SVG is not displaying in android canvas - android

I have eps image files. i have converted to .svg by online converter nut now this converted svg is not getting displayed on the screen. (Code works with other original svg image).
Is there something like we can not use converted svg images in android apps?
Here is my code:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = new GestureImageView(this);
svgImg=sVGParser.getSVGFromResource(getResources(),R.raw.american);
view.setImageDrawable(svgImg.createPictureDrawable());
view.setLayoutParams(params);
view.setMaxScale(15f);
ViewGroup layout = (ViewGroup) findViewById(R.id.layout);
layout.addView(view);
}

More than likely the problem is with the SVG generated by the online converter you used.
It appears as if you are using svg-android. You might have more success with AndroidSVG which supports more of the SVG spec.

I would personally suggest you not to use SVG as Android does not support SVG rasterization, you may like to read more discussion here.
but if you really got stick with svg then here are some useful solutions may help you.
Scaling SVGs in Android
Is there any lite SVG viewer for Android
svg image files android
Here are some beautiful tutorials for display SVG in android
Android ImageView and Drawable with SVG Support
Android. ImageView with SVG Support
I could have created a sample code for this but I am in Xcode now.

Related

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 !!

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.

How do I render an SVG in an Android app?

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*/)

SVG image not display

i have converted a psd file into svg, it works good at browser but not works on android native applications, how can i do this?
ImageView imageView = (ImageView)findViewById(R.id.img1);
imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
//Parse the SVG file from the resource
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android);
//Get a drawable from the parsed SVG and apply to ImageView
imageView.setImageDrawable(svg.createPictureDrawable());
This is the code i used to display svg image. it works when i used simple svg images, but it not works converted svg images using illustrator . how can i do?
I am guessing that your SVG just contains the image from the PSD. Is that the case?
If all that the SVG contains is a bitmap image, then you are not really using SVGs for their proper purpose (vector art). You would be better off just converting the PSD to a JPEG or PNG and loading that into an ImageView.
However, if you are definitely sure you want to load an SVG, then the solution depends on which SVG library you are using.
svg-android: As far as I know, it doesn't support <image> elements, so there is no solution
AndroidSVG: supports <image>, so it should work as long as the device has the memory to load the image. If the image is embedded in the file, you should be fine. If it references an external image, you will need to pass in an SVGExternalFileResolver so it knows how to find the bitmap. See my answer to the following question: https://stackoverflow.com/a/21531168/1292848

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