Using SVG as wallpaper on Android

I am trying to use an SVG image (created using Inkscape and saved as a simple SVG) as the background for my application. I am trying to do this using the svg-android library. I have a file called background.svg in res/raw . My code is as follows:

 SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.background); Drawable pictureDrawable = svg.createPictureDrawable(); Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap); LinearLayout backgroundLayout = (LinearLayout) findViewById(R.id.background); bitmapDrawable.setTileModeX(Shader.TileMode.REPEAT); backgroundLayout.setBackgroundDrawable(bitmapDrawable); 

However, when my application starts, nothing is displayed as a background (except for the background color from the layout). My layout xml file is as follows:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#aacceeff" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/background" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> > </LinearLayout> </LinearLayout> 

UPDATE

There seems to be a problem with my SVG. This may be due to the fact that all functions are not supported.

+11
android background svg
Oct 22 2018-11-11T00:
source share
2 answers

The svg-android project has not been updated for more than a year and does not support SVG1.2, therefore svgs created by Inkscape (open-source) are not supported.

However, there is a new svg library for Android: AndroidSVG

They are on version 1.2, and are currently working on 1.3. Including only the jar library, you can programmatically include svgs in Android applications. Almost all svg features are included. I have yet to find svg, which I could not include in this library.

If you include androidsvg from the source code (hg clone) in your project as a library module, you get the SVGImageView class, which is an ImageView extension through which you can add svg to your project using xml files, for example:

 <com.caverock.androidsvg.SVGImageView xmlns:svg="http://schemas.android.com/apk/res-auto" android:layout_width="100dp" android:layout_height="50dp" svg:svg="filename.svg"/> 

What is it. All you have to do is place filename.svg in the resource folder and it will be convenient for you to go.

It supports API 8 and above. There were several problems when using it for API <11 but I was able to fix them. I posted them as problems on the project page, and the authors answered within a few minutes. They are added to the next revision. If you encounter problems with resolving issues, I cannot answer the questions here.

PS The documentation and examples on the project page are excellent, and the library is happy to work. Android and svg are a powerful combination.

+14
Nov 02 '13 at 14:24
source share

I tried the example using the following code and it shows the background correctly:

 LinearLayout root = (LinearLayout) findViewById(R.id.background); SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android_body); Drawable pictureDrawable = svg.createPictureDrawable(); root.setBackgroundDrawable(pictureDrawable); 

Have you tried with another svg?

+1
Oct 22 2018-11-21T00:
source share



All Articles