【发布时间】:2011-12-13 07:58:42
【问题描述】:
我正在尝试使用 SVG 图像(使用 Inkscape 创建并保存为纯 SVG)作为我的应用程序的背景。我正在尝试使用svg-android 库来做到这一点。我在res/raw 中有一个名为background.svg 的文件。我的代码如下所示:
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 似乎有问题。这可能是因为并非所有功能都受支持。
【问题讨论】:
-
您是否尝试过将Drawable pictureDrawable 设置为布局的背景而不是BitmapDrawable bitmapDrawable?
-
@DimitrisMakris 是的,这是我尝试的第一件事,但我仍然有空白背景。另一个问题是无法在
PictureDrawable上设置平铺。 -
以防万一;- 你知道 android 本身不支持 SVG 在适用于 Android 的 firefox 上试试你的代码吗?
-
是的,不是原生的,但是 svg-android 库可以帮助您在 android 上使用 SVG。
标签: android background svg