【问题标题】:Using an SVG as a background drawable in Android在 Android 中使用 SVG 作为背景可绘制对象
【发布时间】: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


【解决方案1】:

svg-android 项目一年多未更新,不支持 SVG1.2,因此不支持 Inkscape(开源)生成的 svg。

不过有一个新的 android svg 库: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 放在资产文件夹中即可。

它支持 API 8 及更高版本。将它用于 API

附:项目页面上的文档和示例非常好,使用该库是一种乐趣。 Android 和 svg 是强大的组合。

【讨论】:

    【解决方案2】:

    我使用以下代码尝试了一个示例,它正确显示了背景:

    LinearLayout root = (LinearLayout) findViewById(R.id.background);
    SVG svg = SVGParser.getSVGFromResource(getResources(),
                    R.raw.android_body);
    Drawable pictureDrawable = svg.createPictureDrawable();
    root.setBackgroundDrawable(pictureDrawable);
    

    您是否尝试过使用其他 svg?

    【讨论】:

    • 我确实尝试在浏览器中加载 SVG,它似乎显示得很好,所以它看起来没有任何问题。让我尝试另一个 svg。哦,你也尝试过像我这样的嵌套布局吗?我想知道这是否导致了问题。
    • 是的,完全一样的布局,但是是的,这无关紧要。我使用的 svg 来自 svg-android 项目(动态壁纸项目)
    • 我用他们提供的 svg 进行了尝试,它似乎有效。所以我想这是我的 svg 的问题。
    猜你喜欢
    • 2011-03-10
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    • 2016-02-21
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多