简体   繁体   English

如何在 Android 中使用 XML 作为可绘制对象创建自定义形状?

[英]How to create custom shape using XML as a drawable in Android?

Have a look at this Gist.看看这个要点。

Above gist creates a drawable which looks like this:上面的要点创建了一个如下所示的可绘制对象:

在此处输入图片说明

Clearly in the gist, pathData property has been provided coordinates.显然,在要点中, pathData属性已提供坐标。 How is this achieved, I mean, ofcourse someone doesn't found each point according to his needs.这是如何实现的,我的意思是,当然有人不会根据自己的需要找到每个点。

My question : How is this achieved?我的问题:这是如何实现的? Is there any tool present to do so?是否有任何工具可以这样做?

Drawable like this reduces app size by avoiding to have an image instead.像这样的 Drawable 通过避免使用图像来减少应用程序的大小。 Also, there are questions for drawables in XML like this , this and many more but I think my Question is very different (and useful, probably).此外,还有一些关于 XML 中的 drawable 的问题,例如thisthis等等,但我认为我的问题非常不同(并且可能很有用)。

You can design your SVG files using some tools such as Android Vector Asset Studio你可以使用一些工具来设计你的 SVG 文件,比如Android Vector Asset Studio

Usually people don't write the SVG paths themselves, they use a tool or an editor where they sketch the drawing and generate the file.通常人们不会自己编写 SVG 路径,他们会使用工具或编辑器来绘制图形并生成文件。

What you have to do is to create a SVG image using an SVG editor such as Inkscape (the one I use) or if you want a more professional (and expensive) editor you can use Adobe Illustrator .您需要做的是使用 SVG 编辑器(例如Inkscape (我使用的))创建 SVG 图像,或者如果您想要更专业(和昂贵)的编辑器,您可以使用Adobe Illustrator You can even use a tool called svg-edit which is accessible in the browser.您甚至可以使用可在浏览器中访问的名为svg-edit的工具。

After creating your required art, save your SVG file.创建所需的艺术作品后,保存您的 SVG 文件。 Next goto and open Android Studio.接下来转到并打开Android Studio。

  • Right click on the res folder (any folder where drawables are usually placed)右键单击 res 文件夹(通常放置可绘制对象的任何文件夹)
  • Select New > Vector Asset.选择新建 > 矢量资源。 This opens the following window.这将打开以下窗口。

在此处输入图片说明

  • Select local file选择本地文件

在此处输入图片说明

  • Edit the path to direct it to the location of your svg file编辑路径以将其定向到 svg 文件的位置
  • Depending on the complexity of your image, errors may be thrown (not all svg elements get converted to xml. Hence, avoid using complex art in svg, use raster images in that case).根据图像的复杂性,可能会抛出错误(并非所有 svg 元素都转换为 xml。因此,避免在 svg 中使用复杂的艺术,在这种情况下使用光栅图像)。
  • Android studio does some fixes on its own. Android Studio 自己做了一些修复。 So even if errors are thrown, if your image renders fine, then you are good to go.因此,即使抛出错误,如果您的图像呈现良好,那么您就可以开始了。
  • Click Next > adjust a few more settings > Click Finish单击下一步> 调整更多设置 > 单击完成
  • Your xml vector asset has been created successfully.您的 xml 矢量资产已成功创建。

Note: To ensure backward compatibility, add the following to your build.gradle注意:为确保向后兼容,请将以下内容添加到 build.gradle

android{
         defaultConfig{
                        vectorDrawables.useSupportLibrary = true
                      }
       }
dependencies{
              compile 'com.android.support:appcompat-v7:23.2.0'
            }

Hope this has helped you.希望这对你有帮助。 For more info see the official documentation .有关更多信息,请参阅 官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM