如何在android中制作渐变背景

kon*_*kea 201 android gradient background shapes

我想创建渐变背景,其中渐变位于上半部分,下半部分是纯色,如下图所示:

我不能因为centerColor展开以覆盖底部和顶部.

在按钮的渐变中,白色水平线在顶部和底部逐渐变为蓝色.

如何制作像第一张图像的背景?如何制作centerColor不散布的小号?

这是上面背景按钮的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>
Run Code Online (Sandbox Code Playgroud)

Pra*_*rma 292

试试这个:

<?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>
Run Code Online (Sandbox Code Playgroud)

  • @Ankit:你制作了一个可绘制的.xml文件,将上面的代码复制并粘贴到这个文件中,很有趣.<RelativeLayout ... android:background ="@ drawable/your_xml_name"...> (2认同)

Sur*_*gch 273

视觉示例有助于解决这类问题.

样板

要创建渐变,请在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>
Run Code Online (Sandbox Code Playgroud)

您将其设置为某个视图的背景.例如:

<View
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:background="@drawable/my_gradient_drawable"/>
Run Code Online (Sandbox Code Playgroud)

类型="线性"

设置anglelinear类型.它必须是45度的倍数.

<gradient
    android:type="linear"
    android:angle="0"
    android:startColor="#f6ee19"
    android:endColor="#115ede" />
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

类型="径向"

设置gradientRadiusradial类型.使用%p意味着它是父母最小维度的百分比.

<gradient
    android:type="radial"
    android:gradientRadius="10%p"
    android:startColor="#f6ee19"
    android:endColor="#115ede" />
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

类型="扫"

我不知道为什么有人会使用扫描,但我要包括它以保持完整性.我无法弄清楚如何改变角度,所以我只包括一个图像.

<gradient
    android:type="sweep"
    android:startColor="#f6ee19"
    android:endColor="#115ede" />
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

中央

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

android:centerX="0.2"
android:centerY="0.7"
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 完美解释了。谢谢 (3认同)

Gun*_*son 54

您可以使用xml Layer-List将顶部和底部"band"组合到一个文件中,从而创建这种"半渐变"外观.每个乐队都是xml形状.

有关详细教程,请参阅上一个关于SO的答案:多梯度形状.


Hit*_*its 47

以下链接可以帮助您http://angrytools.com/gradient/.这将在Android中创建自定义渐变背景,就像在photoshop中一样.


Har*_*ish 30

首先,您需要创建一个gradient.xml,如下所示

<shape>
    <gradient android:angle="270" android:endColor="#181818" android:startColor="#616161" />

    <stroke android:width="1dp" android:color="#343434" />
</shape>
Run Code Online (Sandbox Code Playgroud)

然后你需要在布局的背景中提到上面的渐变.如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/gradient"
    >   
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)


mir*_*ign 21

或者您可以在代码中使用您在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);
    }
Run Code Online (Sandbox Code Playgroud)


小智 6

//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);
Run Code Online (Sandbox Code Playgroud)

请参阅 https://android--code.blogspot.in/2015/01/android-button-gradient-color.html


Mut*_*nan 6

在 drawable 文件夹中使用此代码。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#3f5063" />
    <corners
        android:bottomLeftRadius="30dp"
        android:bottomRightRadius="0dp"
        android:topLeftRadius="30dp"
        android:topRightRadius="0dp" />
    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp" />
    <gradient
        android:angle="45"
        android:centerColor="#015664"
        android:endColor="#636969"
        android:startColor="#2ea4e7" />
    <stroke
        android:width="1dp"
        android:color="#000000" />
</shape>
Run Code Online (Sandbox Code Playgroud)