简体   繁体   English

如何在 Android XML 中绘制圆弧?

[英]How Can I Draw an Arc in Android XML?

I want to draw an arc as an Android XML object.我想绘制一个弧作为 Android XML 对象。 I need it to be an xml, not a Drawable I can code programmatically (that'd be too easy!) because I need it for a notification icon.我需要它是一个 xml,而不是一个 Drawable 我可以以编程方式编码(这太容易了!)因为我需要它作为通知图标。

I know how to code circles and rings, and I can use radius on rectangles to get quarter/half circles fine.我知道如何对圆和环进行编码,并且我可以在矩形上使用半径来获得四分之一/半圆。 However, I can't figure out how to code just any arc (eg 30 degrees) in an xml.但是,我不知道如何在 xml 中对任何弧(例如 30 度)进行编码。 Is it possible?是否可以?

that XML is still a Drawable resource, while SVG is being called vector & path in there. XML仍然是一个Drawable资源,而SVG在那里被称为vectorpath you can use whatever tool to create vector paths, which then can be used as the pathData .您可以使用任何工具来创建矢量路径,然后可以将其用作pathData the correct location for such files is src/main/res/drawable-nodpi , here's one example path (and one can put several of them into such a vector ):此类文件的正确位置是src/main/res/drawable-nodpi ,这是一个示例path (可以将其中的几个放入这样的vector ):

<?xml version="1.0" encoding="utf-8"?>
<vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:viewportWidth="500.0"
    android:viewportHeight="500.0"
    android:width="50dp"
    android:height="50dp">

    <path
        android:fillColor="@color/colorAccent"
        android:pathData="M 250 250 A 100 100 0 0 0 450 250 Z"/>

</vector>

the actual know "how to draw an arc" (with the values of the above path):实际知道“如何绘制弧线”(使用上述路径的值):

矢量路径构建

meanwhile Android Studio even has a "Preview" tab for that:与此同时,Android Studio 甚至有一个“预览”标签:

AS 截图,XML 预览

For me, the solution was quite simple.对我来说,解决方案非常简单。 I had to use Figma to create the Arc.我不得不使用 Figma 来创建 Arc。

Note: It does not matter whether or not you have prior experience with using Figma or any design tool for that matter.注意:您是否有使用 Figma 或任何设计工具的经验并不重要。 It is a very simple and straightforward process and it takes less than 5 minutes to achieve.这是一个非常简单直接的过程,只需不到 5 分钟即可完成。


Now, let's get to the good part...现在,让我们进入好的部分......

  • The first step is to download the Figma Desktop app and draw an arc.第一步是下载 Figma 桌面应用程序并绘制一条弧线。 This short video pretty much summarizes how to draw an arc.这个简短的视频几乎总结了如何绘制弧线。

  • The second step is to export your arc as an SVG file.第二步是将弧导出为 SVG 文件。 Again, this is a straightforward process, this 51-second video summarizes this process.同样,这是一个简单的过程,这个 51 秒的视频总结了这个过程。

  • The last step is to add the SVG file to Android studio.最后一步是将 SVG 文件添加到 Android Studio。 To do that, go to File > New >Vector Asset and then select Local File in the menu that pops up.为此,请转到“文件”>“新建”>“矢量资产” ,然后在弹出的菜单中选择“本地文件”。 All in all, you should see a menu like the one below.总而言之,您应该会看到如下所示的菜单。 Note that the name of my SVG local file is arc.svg.请注意,我的 SVG 本地文件的名称是 arc.svg。

在此处输入图片说明

Arc is just like a quarter-circle .弧形就像一个四分之一圆 So, you need to draw rectangle shape to form an arc.因此,您需要绘制矩形以形成圆弧。 Below code is to draw an arc using rectangle shape :下面的代码是使用矩形绘制圆弧:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:height="15dp" android:width="15dp"/>
<solid android:color="@color/darkGray"/>
<corners android:topRightRadius="0dp" android:bottomRightRadius="15dp"/>
</shape>

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

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