Android:使用形状绘制“X”

时间:2012-06-18 16:22:39

标签: android xml drawing shape

需要使用 Shapes View中绘制“X”,但X的边缘必须锚定在视图的左侧,顶部,右侧和底部。

这样的事情:

sample image

3 个答案:

答案 0 :(得分:7)

通过创建自定义视图并覆盖onDraw方法,可以更轻松地完成此操作。即

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);
    }
}

答案 1 :(得分:6)

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>

使用填充:

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

答案 2 :(得分:0)

我有一个类似的场景,但我的盒子是预定义的高度和宽度,仅使用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  

输出:
enter image description here