Android自定义形状

Android自定义形状,android,shape,Android,Shape,我知道可以使形状看起来像这样: 但我不知道如何开始。我能把它做成一个形状吗?还是我必须做点别的 BR您可以在形状文件中用xml定义它,但制作一个简单的9面片图形可能要容易得多,然后您可以轻松自定义弯曲和直线段拉伸的方式和位置 有关更多信息,请参阅 编辑 有关形状的更多信息,请参见此处:请参阅此文档以了解您需要使用的形状 以下是根据您的图像显示的代码: 自定义图层列表.xml <layer-list xmlns:android="http://schemas.android.com

我知道可以使形状看起来像这样:

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


BR

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

有关更多信息,请参阅

编辑

有关形状的更多信息,请参见此处:

请参阅此文档以了解您需要使用的形状

以下是根据您的图像显示的代码:

自定义图层列表.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>
<?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>
<?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>

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

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

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

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

有关渐变填充的详细信息,请参见此处:

您只能使用形状:

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


是的,我可以。。。但我不想这样做,这样我就可以确保无论它运行在哪个设备上,图像都是干净清晰的。我想这里最大的问题是:它是干什么用的?从这一点,我们可以确定做这件事的最佳方式。如果你只需要形状,我会覆盖视图的ondraw,画一个椭圆和一个相同颜色的矩形。这种方法的一个可能的问题是,如果你想用渐变填充。嘿,我会用它作为一些按钮后面的背景。是的。。。我将有一个渐变。我想在代码中创建它,这样我确信它在所有设备上都会保持清晰。我如何将这个添加到我的布局中?嗯。当我使用align_parrent_buttom=true时,视图消失;我做错了什么?嗯,不确定-可能需要几次尝试才能找到从中继承的正确成员-如果您将“extends View”更改为“extends TextView”,它应该仍然很好,也许这会解决它。如果没有,请尝试“扩展线性布局”在某处感谢某人-您将很快收到您的斗篷和超级英雄卡。当然,这段代码可能只是一个开始的例子,需要开发人员运用自己的一些脑力。我也有同样的问题。我已经创建了一个形状,但无法在布局中使用它。请建议此代码在我这边测试。请在你这边检查。以上三个xml文件都放在drawable文件夹中。@Vinet Shukla rect.xml不起作用,但topcircular.xml足以满足此问题中的形状要求。