【问题标题】:Android custom shape安卓自定义形状
【发布时间】:2012-07-31 11:11:51
【问题描述】:

我知道可以制作如下所示的形状:

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

BR

【问题讨论】:

  • 我认为这里最大的问题是:它是干什么用的?从中我们可以确定执行此操作的最佳方法。如果您只需要形状,我只需覆盖视图的 ondraw 并绘制一个椭圆和一个相同颜色的矩形。这种方法的一个可能问题是,如果您想填充渐变。
  • 嘿,我会用它作为一些按钮后面的背景。是的……我会在上面做一个渐变。
  • 我想用代码创建它,所以我相信它会在所有设备上保持清晰。

标签: android shape


【解决方案1】:

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

更多信息请见http://developer.android.com/tools/help/draw9patch.html

编辑

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

【讨论】:

  • 是的,我可以做到...但我不想这样做,所以我可以确保无论在哪个设备上运行,图像都会干净清晰。
【解决方案2】:

请参阅此文档以获取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>

【讨论】:

  • 这段代码在我这边测试过。请在你身边检查。以上三个xml文件都在drawable文件夹中。
  • @Vineet Shukla rect.xml 不起作用,但 topcircular.xml 足以满足此问题中要求的形状。
【解决方案3】:

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

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?

【讨论】:

  • 如何将这个添加到我的布局中?
  • 嗯。当我使用 align_parrent_buttom = true; 时视图消失了我做错了什么?
  • 嗯,不确定 - 可能需要尝试几次才能找到合适的成员来继承 - 如果您将“扩展视图”更改为“扩展 TextView”,它应该仍然可以正常绘制,也许这会解决它。如果没有尝试“扩展线性布局”
  • 在某处感谢某人 - 您很快就会收到您的披风和超级英雄卡片。当然,这段代码可能只是一个示例,需要开发者自己发挥一些脑力。
  • 我也有同样的问题。我创建了一个形状,但无法在布局中使用它。请建议
【解决方案4】:

你只能用形状来做到这一点:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    • 2019-02-28
    • 2013-06-18
    相关资源
    最近更新 更多