我正在尝试使用SVG映像(使用Inkscape创建并保存为普通SVG)作为我的应用程序的背景。我正在尝试使用svg-android库来完成这个任务。我有一个名为background.svg in res/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);但是,当我的应用程序启动时,没有任何东西显示为背景(除了布局中的背景色)。我的布局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>更新
看来我的SVG有问题。这可能是因为不支持所有功能。
发布于 2013-11-02 14:24:35
SVG1.2项目已经有一年多没有更新了,而且它不支持SVG1.2,因此不支持Inkscape (开源)生成的svgs。
然而,有一个新的android库:AndroidSVG
它们的版本为1.2,1.3的工作目前正在进行中。仅包括jar库,就可以编程地在android应用程序中包含svgs。几乎所有svg特性都包括在内。我还没有找到一个svg,我无法结合使用这个库。
如果在项目中将androidsvg从源(hg克隆)中包含为库模块,则会得到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"/>就这样。您所需要做的就是将filename.svg放置在assets文件夹中,这样就可以了。
它支持API 8和更高版本。在API < 11中使用它时存在一些问题,但我能够修复这些问题。我把它们作为问题张贴在项目页面上,几分钟内作者就做出了回应。它们已被添加到下一次修订中。如果你有任何问题,看看解决的问题,否则我可以在这里回答问题。
项目页面上的文档和示例都很优秀,图书馆是joy的作品。Android和svg是一个强大的组合。
发布于 2011-10-22 21:17:05
我使用以下代码尝试了一个示例,它正确地显示了背景:
LinearLayout root = (LinearLayout) findViewById(R.id.background);
SVG svg = SVGParser.getSVGFromResource(getResources(),
R.raw.android_body);
Drawable pictureDrawable = svg.createPictureDrawable();
root.setBackgroundDrawable(pictureDrawable);你试过用另一个svg吗?
https://stackoverflow.com/questions/7862493
复制相似问题