使用SVG作为Android的背景绘制 [英] Using an SVG as a background drawable in Android

查看:992
本文介绍了使用SVG作为Android的背景绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用SVG图像(使用Inkscape中创建并保存为纯SVG)作为背景,我的应用程序。我试图做到这一点使用 SVG-机器人库。我有 background.svg 水库称为文件/原。我的code是这样的:

I'm trying to use an SVG image (created using Inkscape and saved as plain SVG) as a background for my application. I'm trying to do this using the svg-android library. I have a file called background.svg in res/raw. My code looks like this:

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文件如下:

However when my application starts up, nothing shows up as the background (other than the background color from the layout). My layout xml file is as follows:

<?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的一个问题。这可能是因为,不支持的所有功能都这一事实。

It appears that there is a problem with my SVG. It might be due to the fact that all features are not supported.

推荐答案

在SVG-Android项目已经没有更新了一年多,它不支持SVG1.2所以svgs Inkscape中所产生的(开放源代码)不支持。

The svg-android project hasn't been updated in over a year and it doesn't support SVG1.2 so svgs generated by Inkscape (open-source) aren't supported.

然而,有一个新的Andr​​oid SVG库: AndroidSVG

There is however a new android svg library: AndroidSVG

它们是对1.3 1.2版和工作目前正在进行中。包括刚才的jar库中的一个可以编程包括Android应用程序svgs。几乎所有的SVG功能都包括在内。我还没有找到一个SVG我无法使用这个库合并。

They are on version 1.2 and work on 1.3 is currently in progress. Including just the jar library one can programatically include svgs in android applications. Almost all svg features are included. I am yet to find an svg that I was unable to incorporate using this library.

如果你包括从源头(HG克隆)在您的项目androidsvg当你是ImageView的的扩展,让你可以使用XML布局文件,像这样添加SVG到项目的SVGImageView类库模块:

If you include androidsvg from source (hg clone) in your project as a library module you get the SVGImageView class which is an extension of ImageView whereby you can add svg to your project using the xml layout files like so:

<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 的资产的文件夹,你是好去。

That's it. All you need to to do is place filename.svg in the assets folder and you are good to go.

它支持API 8及以上。有时候,使用它的API℃的几个问题; 11但我可​​以修复这些。我贴他们的项目主页上的问题,作者在几分钟之内作出答复。它们已被添加到下一版本。如果你有任何问题,看看解决的问题,否则,我可以在这里回答问题。

It supports API 8 and above. There were a few issues when using it for API < 11 but I was able to fix these. I posted them as issues on the project page and the authors responded within minutes. They have been added to the next revision. If you have any problems look at the resolved issues, failing which I am available to answer questions here.

P.S。该项目页面上的文档和示例是优秀的和库是一种快乐的工作。 Android和SVG是一个强大的组合。

P.S. The documentation and examples on the project page are excellent and the library is a joy to work with. Android and svg are a powerful mix.

这篇关于使用SVG作为Android的背景绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆