【发布时间】:2022-01-21 15:39:05
【问题描述】:
【问题讨论】:
-
不要在 xml 中那样做,浪费时间,而是使用
CanvasAPI 绘制它
标签: android android-layout android-xml
【问题讨论】:
Canvas API 绘制它
标签: android android-layout android-xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="52"
android:toDegrees="45"
android:pivotX="-45%"
android:pivotY="90%" >
<shape
android:shape="rectangle" >
<solid
android:color="@color/colorPrimary"/>
</shape>
</rotate>
</item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="-50"
android:toDegrees="45"
android:pivotX="140%"
android:pivotY="90%" >
<shape
android:shape="rectangle" >
<solid
android:color="@color/colorPrimary"/>
</shape>
</rotate>
</item>
</layer-list>
【讨论】:
fromDegrees 和toDegrees 将矩形旋转为菱形或菱形,然后将形状移出使用pivotX 和pivotY 查看以使所需的形状区域可见,在本例中为三角形。因此,它始终是一个 Rectangle 但明显是三角形,因为其他部分超出了视野范围并且不可见。要将矩形的右侧或左侧旋转为菱形/风筝形状,我们使用fromDegrees 和toDegrees,然后相应地移动它。
试试这个
还可以查看我的另一个答案How to create vector drawable in android studio
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="113dp"
android:viewportWidth="108"
android:viewportHeight="113">
<path
android:pathData="M2.423,111.534L105.599,110.724L1.032,3.022L2.423,111.534Z"
android:strokeWidth="2"
android:fillColor="#ffffff"
android:strokeColor="#000000"/>
</vector>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="119dp"
android:height="126dp"
android:viewportWidth="119"
android:viewportHeight="126">
<path
android:strokeWidth="1"
android:pathData="M117.632,124.928L1.686,125.144L118.003,2.092L117.632,124.928Z"
android:fillColor="#ffffff"
android:strokeColor="#000000"/>
</vector>
【讨论】:
用它来画线。
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/textView4"
android:layout_marginTop="210dp"
android:background="@color/cardview_dark_background"/>
你有线条,现在画三角形没什么大不了的。
【讨论】: