Using an SVG as a background drawable in Android

后端 未结 2 391
清酒与你
清酒与你 2021-02-04 06:40

I\'m trying to use an SVG image (created using Inkscape and saved as plain SVG) as a background for my application. I\'m trying to do this using the svg-android lib

相关标签:
2条回答
  • 2021-02-04 07:04

    The svg-android project hasn't been updated in over a year and it doesn't support SVG1.2 so svgs generated by Inkscape (open-source) aren't supported.

    There is however a new android svg library: AndroidSVG

    They are on version 1.2 and work on 1.3 is currently in progress. Including just the jar library one can programatically include svgs in android applications. Almost all svg features are included. I am yet to find an svg that I was unable to incorporate using this library.

    If you include androidsvg from source (hg clone) in your project as a library module you get the SVGImageView class which is an extension of ImageView whereby you can add svg to your project using the xml layout files like so:

    <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"/>
    

    That's it. All you need to to do is place filename.svg in the assets folder and you are good to go.

    It supports API 8 and above. There were a few issues when using it for API < 11 but I was able to fix these. I posted them as issues on the project page and the authors responded within minutes. They have been added to the next revision. If you have any problems look at the resolved issues, failing which I am available to answer questions here.

    P.S. The documentation and examples on the project page are excellent and the library is a joy to work with. Android and svg are a powerful mix.

    0 讨论(0)
  • 2021-02-04 07:07

    I tried an example using the following code and it is 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?

    0 讨论(0)
提交回复
热议问题