安卓渐变中的角度属性

82

我正在进行测试示例。在某些图像背景中,它们使用渐变,代码如下:

<?xml version="1.0" encoding="utf-8"?>


  <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"
        android:angle="180"/>
    <corners android:radius="5dp" />
   </shape>

在上述XML中,我没有得到angle属性。但是当我稍微改变angle的值时,图案会倾斜。有人能解释一下它是如何工作的吗?

5个回答

185

渐变基本上表示任何数量在空间中(某个方向上)的变化。用颜色来表示颜色强度在角度方向上的变化。以下是一些用于表示这个概念的图示:
enter image description here

此处的图形显示了水平方向的颜色变化(角度设置为0)。
XML代码:

    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#000000"
        android:angle="0"/>
   </shape>

在这里输入图片描述

这里的图示展示了垂直方向上的颜色变化(角度设置为90)。
XML代码:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
    android:startColor="#000000"
    android:angle="90"/>
 </shape>

你也可以使用不同的颜色作为起始、中间和结束颜色。你附加的代码包含了所有这些元素。


32
谢谢你的回答,Karn。但我发现在角度属性中我只能输入45的倍数,否则会出错,例如20、20、50等。 - Sharanabasu Angadi
2
作为起始颜色,黑色(#000000)似乎箭头方向相反,不是吗? - lm2a
@Im2a 起始点在左侧,然后逆时针方向旋转。只需尝试更改您的xml中的角度,您就会看到它发生了变化。 - karn
7
我很困惑你在这里的回答。起点是从左到右,逆时针方向移动。所以在图像1中,我期望箭头移动像这样->->->->。另外,我对“android: startColor =“#000000””的工作方式感到困惑。如果我们将起始角度设为90,这是否意味着从角度90-270颜色将是黑色?那么剩下的角度(0-90)呢? - Edijae Crusar
2
@gikarasojo kinene 答案是正确的。但是,从三角学的角度来看,描述角度的向量始于原点(坐标的起点)。角度是逆时针方向测量的。因此,当角度为90(pi/2)时,正交向量朝上。因此,上面答案中的箭头方向是错误的。相反,第一张图片也是如此。 - CodeToLife
显示剩余3条评论

18

指定形状的渐变颜色。

属性:

android:angle 整数。渐变的角度,以度为单位。0表示从左到右,90表示从下到上。必须是45的倍数。默认值为0。

看起来文档中的描述与karn的答案相矛盾?

您可以在文档中找到更多详细信息。


4
你说“必须是45的倍数”时,我就已经被吸引了。 - Pierre
有没有办法在不使用SWEEP_GRADIENT的情况下避开这个45度角规则? - Mr.Drew
@Mr.Drew 您可以使用LinearGradient的前4个参数创建不同的角度,如链接所述。之后使用linearGradient.setLocalMartix()将其缩放到所需的大小。 - Julian Fisch
安卓9及以下的角度属性默认为0。而在安卓10中,则为270 官方文档 - Myroslav

17
更简单地说,给出相对于您想要开始的点的角度值。\{\{end\}\}

enter image description here

根据角度值,它将以startColor为起点开始。

例如,当角度为90时:

enter image description here

270的示例:

enter image description here


1
这是最好的答案。它真的帮助我理解梯度角是如何工作的。 - Nabeel Ahmed
1
它很有帮助,易于理解,感谢指引。 - Rucha Bhatt Joshi

12

你可能想要从代码中创建对角渐变。这样更容易,并且您有很多选项可以选择。这个片段帮助了我。


public void SetGradient(View view) {
        GradientDrawable gd = new GradientDrawable(
                GradientDrawable.Orientation.TL_BR,
                new int[]{0xFF141a24, 0xFF293f49, 0xFF72554c});
        view.setBackground(gd);
    }

GradientDrawable类提供的可用方向

/*public enum Orientation {
        *//** draw the gradient from the top to the bottom *//*
        TOP_BOTTOM,
        *//** draw the gradient from the top-right to the bottom-left *//*
        TR_BL,
        *//** draw the gradient from the right to the left *//*
        RIGHT_LEFT,
        *//** draw the gradient from the bottom-right to the top-left *//*
        BR_TL,
        *//** draw the gradient from the bottom to the top *//*
        BOTTOM_TOP,
        *//** draw the gradient from the bottom-left to the top-right *//*
        BL_TR,
        *//** draw the gradient from the left to the right *//*
        LEFT_RIGHT,
        *//** draw the gradient from the top-left to the bottom-right *//*
        TL_BR,
    }*/

你需要在fragment的onCreate或onCreateView方法中调用该方法,并传递父视图(在我的情况下)。

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_view_parent, container);           
        ...

        SetGradient(view);

        return view;
    }

1
我制作了一种通用解决方案,支持喷气背包上的任何角度,并撰写了一篇关于它的中等文章(感谢第一个解决方案提供的思路)。如果需要,请查看。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接