Android自定义形状

时间:2012-08-02 13:13:34

标签: android shape

我知道有可能使形状看起来像这样: enter image description here

但我不知道如何开始。我可以把它做成一个形状吗?还是我必须做别的事?

BR

4 个答案:

答案 0 :(得分:4)

请参阅此details的文档,您需要使用Layer List

以下是根据您的图片编写的代码:

<强> custom_layer_list.xml

<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/topCircular" />

    <item
        android:drawable="@drawable/rect"
        android:top="20dp" />
</layer-list>

<强> topCircular.xml

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

<solid
    android:color="#000000" />
<corners
    android:topLeftRadius="25dp"
    android:topRightRadius= "25dp" />

</shape>

<强> rect.xml

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

<solid
    android:color="#000000" />

</shape>

答案 1 :(得分:4)

哦,看看,我错了 - 渐变不是问题:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.view.View;

public class ButtonShadow extends View {

    public ButtonShadow(Context context)
    {
        super(context);
    }

    @Override
    public void onDraw(Canvas canvas)
    {
        RectF space = new RectF(this.getLeft(), this.getTop(), this.getRight(), this.getBottom());

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        paint.setShader(new LinearGradient(0, getWidth(), 0, 0, Color.BLACK, Color.WHITE, Shader.TileMode.MIRROR));

        canvas.drawArc(space, 180, 360, true, paint);

        Rect rect = new Rect(this.getLeft(),this.getTop() + (this.getHeight() / 2),this.getRight(),this.getBottom());
        canvas.drawRect(rect, paint);
    }
}

有关渐变填充的更多信息,请查看此处:How to fill a Path in Android with a linear gradient?

答案 2 :(得分:1)

您可以在形状文件中以xml定义它,但是制作一个简单的9补丁图形可能要容易得多,然后您可以轻松自定义弯曲和直线段的拉伸方式和位置。

有关详细信息,请参阅http://developer.android.com/tools/help/draw9patch.html

修改

有关形状的更多信息,请参见此处:http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

答案 3 :(得分:1)

你只能用形状来做:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#000000" />
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners  
        android:topLeftRadius="90dip"
        android:topRightRadius="90dip"
        android:bottomLeftRadius="0dip"
        android:bottomRightRadius="0dip" />
</shape>