【问题标题】:How to draw right Triangles shape in android xml如何在android xml中绘制右三角形形状
【发布时间】:2022-01-21 15:39:05
【问题描述】:

如何在android中使用xml创建形状?

【问题讨论】:

  • 不要在 xml 中那样做,浪费时间,而是使用Canvas API 绘制它

标签: android android-layout android-xml


【解决方案1】:

已更新,现在是直角三角形
这肯定会起作用
1

  1. 此代码将生成如上图所示的左侧三角形。


<?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>

2

  1. 此代码将生成如上图所示的右侧三角形。


<?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>

【讨论】:

  • 这行不通。它是一个等边三角形。我想要一个正确的。
  • 你能解释一下后面的登录吗,解决方案很完美,但我不知道我们成功了。
  • @NishchayZacariah 由于三角形不是默认形状,我们首先使用fromDegreestoDegrees 将矩形旋转为菱形或菱形,然后将形状移出使用pivotXpivotY 查看以使所需的形状区域可见,在本例中为三角形。因此,它始终是一个 Rectangle 但明显是三角形,因为其他部分超出了视野范围并且不可见。要将矩形的右侧或左侧旋转为菱形/风筝形状,我们使用fromDegreestoDegrees,然后相应地移动它。
  • 了解拉利特,非常感谢。
【解决方案2】:

试试这个

还可以查看我的另一个答案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>

【讨论】:

    【解决方案3】:

    用它来画线。

    <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"/>
    

    你有线条,现在画三角形没什么大不了的。

    【讨论】:

    • 虽然这个答案确实画了一条线,但没有说明如何旋转这些线并将它们对齐。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多