简体   繁体   中英

Android: drawing an “X” using shape

A need to draw an "X" in a View using Shapes , but the X's edges must be anchored on left, top, right and bottom of the view.

Something like this:

样本图像

By xml at drawable:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item>
    <rotate
        android:fromDegrees="135"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="135">
        <shape android:shape="line">
            <stroke android:width="1dp" android:color="@color/social_grey" />
        </shape>
    </rotate>
</item>
<item>
    <rotate
        android:fromDegrees="45"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="45">
        <shape android:shape="line">
            <stroke android:width="1dp" android:color="@color/social_grey" />
        </shape>
    </rotate>
</item>


</layer-list>

With padding:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item
   android:left="4dp"
    android:right="4dp"
    >
    <rotate
        android:fromDegrees="135"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="135">
        <shape android:shape="line">
            <stroke android:width="1dp" android:color="@color/social_grey" />
        </shape>
    </rotate>
</item>
<item
    android:left="4dp"
    android:right="4dp"
    >
    <rotate
        android:fromDegrees="45"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="45">
        <shape android:shape="line">
            <stroke android:width="1dp" android:color="@color/social_grey" />
        </shape>
    </rotate>
</item>
</layer-list>

It would be much easier to do it by creating a custom View and overriding the onDraw method. Ie.

public class XView extends View {

    @Override
    public void onDraw(Canvas canvas) {
        float width = getMeasuredWidth();
        float height = getMeasuredHeight();
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        canvas.drawLine(0,0,width,height,paint);
        canvas.drawLine(width,0,0,height,paint);
    }
}

I was having a similar scenario but my box was with predefined height and width, did it only using 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="@color/status_expired_color" />

        </shape>

    </item>

    <item android:top="2dp"
        android:left="2dp"
        android:right="2dp"
        android:bottom="2dp">

        <shape android:shape="rectangle">
            <solid android:color="@color/white" />
        </shape>

    </item>

    <item android:top="14sp"
        android:left="6dp"
        android:right="6dp"
        android:bottom="14sp">

        <rotate
            android:fromDegrees="45"
            android:toDegrees="45"
            android:pivotX="50%"
            android:pivotY="50%" >

            <shape android:shape="rectangle">
                <solid android:color="@color/status_expired_color" />
            </shape>

        </rotate>

    </item>

    <item android:top="6dp"
        android:left="14sp"
        android:right="14sp"
        android:bottom="6dp">

        <rotate
            android:fromDegrees="45"
            android:toDegrees="45"
            android:pivotX="50%"
            android:pivotY="50%" >

            <shape android:shape="rectangle">
                <solid android:color="@color/status_expired_color" />
            </shape>

        </rotate>

    </item>

</layer-list>

@color/status_expired_color = #E9510E
@color/white = #ffffff  

Out put:
在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM