Android: Draw shape with skewed corner

徘徊边缘 提交于 2021-02-20 19:07:51

问题


I want to use a background for my buttons. But when I use a png it slows down the app. Therefore I want to use a xml shape but I do not know how to make the corner cut (like on the picture).

By now I have the following shape which is just a rectangle:

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

How can I draw the bottom right corner?


回答1:


You can try this,

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>

If you want to modify the skewed area you just need to change the value of fromDegrees, pivotX and pivotY value in the second item in skewed_background.xml.




回答2:


This is exactly what you looking for

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

OUTPUT




回答3:


This will give you your desired result

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


来源:https://stackoverflow.com/questions/44564304/android-draw-shape-with-skewed-corner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!