【问题标题】:Android: Draw shape with skewed cornerAndroid:用斜角绘制形状
【发布时间】:2017-06-15 09:48:31
【问题描述】:

我想为我的按钮使用背景。但是当我使用 png 时,它会减慢应用程序的速度。因此我想使用 xml 形状,但我不知道如何进行切角(如图所示)。

现在我有以下形状,它只是一个矩形:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/blue_semi_transparent"/>
    <padding android:bottom="10dp" android:right="10dp" android:top="10dp" android:left="10dp"/>
    <margin android:bottom="10dp" android:right="10dp" android:top="10dp" android:left="10dp"/>
</shape>

如何画出右下角?

【问题讨论】:

    标签: android android-layout shape skew


    【解决方案1】:

    你可以试试这个,

    skewed_background.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item>
            <shape android:shape="rectangle" >
    
    
                <solid android:color="#3F8EEB" />
            </shape>
        </item>
        <item>
            <rotate
                android:fromDegrees="110"
                android:pivotX="90%"
                android:pivotY="90%"
                >
                <shape android:shape="rectangle" >
                    <solid android:color="#FFF" />
                </shape>
            </rotate>
        </item>
    
    </layer-list>
    

    sample_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp"
        android:orientation="vertical" >
    
        <View
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:background="@drawable/triangle1"/>
    
    </LinearLayout>
    

    如果要修改倾斜区域,只需要更改skewed_background.xml中第二项中fromDegrees、pivotX和pivotY的值即可。

    【讨论】:

    【解决方案2】:

    这正是你要找的

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bg"
            android:paddingRight="15dp"
            android:textColor="#ffffff"
            android:layout_centerInParent="true"
    
            android:text="Button" />
    </RelativeLayout>
    

    bg.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
    
        <item>
            <shape android:shape="rectangle">
                <size
                    android:width="100dp"
                    android:height="40dp" />
                <solid android:color="#13a89e" />
            </shape>
        </item>
    
    
        <item
            android:right="100dp"
            android:left="100dp"
            android:top="-100dp"
            android:bottom="-100dp">
            <rotate
                android:fromDegrees="45">
                <shape android:shape="rectangle">
                    <solid android:color="#ffffff" />
                </shape>
            </rotate>
        </item>
    
    
        <item
            android:right="-100dp"
            android:left="100dp"
            android:top="-100dp"
            android:bottom="-100dp">
            <rotate
                android:fromDegrees="45">
                <shape android:shape="rectangle">
                    <solid android:color="#ffffff" />
                </shape>
            </rotate>
        </item>
    
    </layer-list>
    

    输出

    【讨论】:

      【解决方案3】:

      这会给你想要的结果

      <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="@color/colorPrimary"/>
      
      
      <item android:top="-20dp"
          android:bottom="-50dp"
          android:left="350dp"
          android:right="-120dp">
          <rotate
              android:fromDegrees="10"
              android:pivotX="50%"
              android:pivotY="0%">
              <shape
                  android:shape="rectangle">
                  <solid
                      android:color="?android:colorBackground"/>
              </shape>
          </rotate>
      </item>
      </layer-list>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-26
        • 2016-09-21
        • 1970-01-01
        • 1970-01-01
        • 2011-12-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多