我想创建一个渐变背景,渐变在上半部分,在下半部分有一个纯色,如下图所示:

我不能,因为中心色展开覆盖底部和顶部。

我如何制作一个像第一张图片一样的背景?我怎么做一个小的中心色,不展开?

这是上面后台按钮的XML代码。

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <gradient 
        android:startColor="#6586F0"
        android:centerColor="#D6D6D6"
        android:endColor="#4B6CD6"
        android:angle="90"/>
    <corners 
        android:radius="0dp"/>


</shape>

当前回答

试试这个:

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

    <gradient
        android:angle="90"
        android:centerColor="#555994"
        android:endColor="#b5b6d2"
        android:startColor="#555994"
        android:type="linear" />

    <corners 
        android:radius="0dp"/>

</shape>

其他回答

//Color.parseColor() method allow us to convert
// a hexadecimal color string to an integer value (int color)
int[] colors = {Color.parseColor("#008000"),Color.parseColor("#ADFF2F")};

//create a new gradient color
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);

gd.setCornerRadius(0f);
//apply the button background to newly created drawable gradient
btn.setBackground(gd);

此处参考 https://android--code.blogspot.in/2015/01/android-button-gradient-color.html

可视化的例子有助于解决这类问题。

样板

为了创建一个渐变,你在res/drawable中创建一个xml文件。我调用我的my_gradient_drawable.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:type="linear"
        android:angle="0"
        android:startColor="#f6ee19"
        android:endColor="#115ede" />
</shape>

你把它设置为某个视图的背景。例如:

<View
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:background="@drawable/my_gradient_drawable"/>

类型=“线性”

设置线性类型的角度。它一定是45度的倍数。

<gradient
    android:type="linear"
    android:angle="0"
    android:startColor="#f6ee19"
    android:endColor="#115ede" />

类型=“放射状”

为径向类型设置gradienttradius。使用%p意味着它是父节点最小维度的百分比。

<gradient
    android:type="radial"
    android:gradientRadius="10%p"
    android:startColor="#f6ee19"
    android:endColor="#115ede" />

类型=“扫描”

我不知道为什么有人会使用扫描,但我包括它的完整性。我不知道如何改变角度,所以我只包括一张图像。

<gradient
    android:type="sweep"
    android:startColor="#f6ee19"
    android:endColor="#115ede" />

中心

还可以更改扫描类型的中心或径向类型。这些值是宽度和高度的分数。您也可以使用%p符号。

android:centerX="0.2"
android:centerY="0.7"

下面的链接可能会帮助你http://angrytools.com/gradient/ .这将在android中创建自定义渐变背景,就像在photoshop中一样。

你可以通过使用xml图层列表来创建这种“半渐变”外观,将顶部和底部的“带”合并到一个文件中。每个带都是一个xml形状。

查看之前关于SO的回答,了解详细教程:多梯度形状。

或者你可以在代码中使用你在PSD中想到的任何东西:

    private void FillCustomGradient(View v) {
        final View view = v;
        Drawable[] layers = new Drawable[1];

        ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {
            @Override
            public Shader resize(int width, int height) {
                LinearGradient lg = new LinearGradient(
                        0,
                        0,
                        0,
                        view.getHeight(),
                        new int[] {
                                 getResources().getColor(R.color.color1), // please input your color from resource for color-4
                                 getResources().getColor(R.color.color2),
                                 getResources().getColor(R.color.color3),
                                 getResources().getColor(R.color.color4)},
                        new float[] { 0, 0.49f, 0.50f, 1 },
                        Shader.TileMode.CLAMP);
                return lg;
            }
        };
        PaintDrawable p = new PaintDrawable();
        p.setShape(new RectShape());
        p.setShaderFactory(sf);
        p.setCornerRadii(new float[] { 5, 5, 5, 5, 0, 0, 0, 0 });
        layers[0] = (Drawable) p;

        LayerDrawable composite = new LayerDrawable(layers);
        view.setBackgroundDrawable(composite);
    }