我想使用SVG图像(使用Inkscape创建并保存为纯SVG)作为我的应用程序的背景。我正在尝试使用svg-android
库来做到这一点。我在res/raw
有一个名为background.svg
的文件。我的代码如下所示:在Android中使用SVG作为背景drawable
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>
UPDATE
看来,有一个与我的SVG的一个问题。这可能是由于所有功能都不受支持。
您是否尝试将可绘制的pictureDrawable设置为布局的背景而不是BitmapDrawable bitmapDrawable? –
@DimitrisMakris是的,那是我尝试的第一件事,但我仍然留有空白背景。另一个问题是,无法在“PictureDrawable”上设置拼贴。 –
以防万一; - ü知道android本身不支持SVG 在Android上使用firefox试试你的代码? – Chasbeen