在Android中使用SVG作为背景绘图

前端之家收集整理的这篇文章主要介绍了在Android中使用SVG作为背景绘图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用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>

UPDATE

看来我的SVG有问题.这可能是因为不支持所有功能.

解决方法

svg-android项目一年没有更新,它不支持SVG1.2,所以不支持Inkscape(开源)生成的svgs.

但是有一个新的android svg库:AndroidSVG

它们在版本1.2上,1.3的工作正在进行中.包括只是jar库可以在Android应用程序中编程地包含svgs.几乎所有的svg功能都包含在内.我还没找到一个无法使用这个库的svg.

如果您在项目中将源代码(hg clone)中的androidsvg作为库模块,您将获得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< 11但我能够解决这些问题.我将它们作为项目页面上的问题发布,作者在几分钟内作出回复.它们已经被添加到下一个版本.如果您有任何问题,请查看已解决的问题,否则我可以在这里回答问题. 附:项目页面上的文档和示例非常出色,图书馆很乐意与之合作. Android和svg是一个强大的组合.

原文链接:https://www.f2er.com/android/311774.html

猜你在找的Android相关文章