三角形作为一个基本的Shape,在Android中是不支持的。
以前我们这样来定义三角形:
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="80%">
<shape android:shape="rectangle">
<solid android:color="#000000"/>
</shape>
</rotate>
将一个矩形旋转一定的角度得到三角形。说实话,这种方式一直都模模糊糊的,所以都拷贝别人代码。
当然也可以自定义View,画一个三角形。
不过今天来说说通过Vector标签(Android对svg的支持方式)来定义三角形:
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:viewportWidth="24"
android:viewportHeight="24"
android:width="24dp"
android:height="24dp">
<path
android:pathData="m0 24 l12 -24 l12 24 z"
android:fillColor="@color/material_red" />
</vector>
其中viewportWidth和viewportHeight属性用于申明坐标的大小,width和height属性标书drawable的大小。
android:pathData 中定位命令:
M = moveto
L = lineto
H = horizontal lineto
V = vertical lineto
C = curveto
S = smooth curveto
Q = quadratic Belzier curve
T = smooth quadratic Belzier curveto
A = elliptical Arc
Z = closepath
注释:以上所有命令均允许小写字母。大写表示绝对定位,小写表示相对定位。
参考:http://www.w3school.com.cn/svg/svg_path.asp
来看看效果: