Android: draw a skewed figure

I want to use the background for my buttons. But when I use png, it slows down the application. Therefore, I want to use the xml form, but I do not know how to make a corner cut (for example, in the picture). enter image description here

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 to draw the bottom right corner?

+4
source share
3 answers

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 change the skew area, you just need to change the fromDegrees, pivotX value and the rotation value in the second element in skewed_background.xml.

+2
source

,

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

enter image description here

+2

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

Source: https://habr.com/ru/post/1679330/


All Articles