使用SVG作为Android中的背景绘制

Viv*_*ath 14 svg android background

我正在尝试使用SVG图像(使用Inkscape创建并保存为普通SVG)作为我的应用程序的背景.我正在尝试使用该svg-android库.我有一个名为文件background.svgres/raw.我的代码看起来像这样:

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);
Run Code Online (Sandbox Code Playgroud)

但是,当我的应用程序启动时,没有任何内容显示为背景(除了布局中的背景颜色).我的布局xml文件如下:

<?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>
Run Code Online (Sandbox Code Playgroud)

UPDATE

看来我的SVG存在问题.这可能是因为不支持所有功能.

Abi*_*aba 18

svg-android项目在一年多时间内没有更新,它不支持SVG1.2,因此不支持由Inkscape(开源)生成的svgs.

但是有一个新的android svg库:AndroidSVG

它们是1.2版本,1.3版本正在进行中.只包括jar库,可以编程方式在android应用程序中包含svgs.几乎所有的svg功能都包含在内.我还没找到一个我无法使用这个库合并的svg.

如果在项目中将源代码(hg clone)中的orroidsvg作为库模块包含,则可以获得SVGImageView类,它是ImageView的扩展,您可以使用xml布局文件将svg添加到项目中,如下所示:

<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"/>
Run Code Online (Sandbox Code Playgroud)

而已.你需要做的就是放在filename.svgassets文件夹中,你很高兴.

它支持API 8及更高版本.将它用于API <11时存在一些问题,但我能够解决这些问题.我在项目页面上将它们作为问题发布,作者在几分钟内回复.它们已被添加到下一版本中.如果您有任何问题,请查看已解决的问题,否则我可以在此处回答问题.

PS项目页面上的文档和示例非常好,图书馆使用起来很愉快.Android和svg是一个强大的组合.