android渐变中的角度属性

Sha*_*adi 66 android gradient android-layout android-shape

我正在通过测试示例.对于某些图像背景,他们使用渐变,代码就像这样

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

在上面的xml我没有得到angle属性.但是当我angle稍微改变模式的值时,模式倾斜.谁能解释我究竟是如何运作的?

kar*_*arn 154

梯度基本上代表任何数量的空间(在一个方向上)的变化.对于颜色,它表示颜色强度在由角度表示的方向上的变化.以下是一些代表这一概念的图表:
在此输入图像描述

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

    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#000000"
        android:angle="0"/>
   </shape>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
    android:startColor="#000000"
    android:angle="90"/>
 </shape>
Run Code Online (Sandbox Code Playgroud)

您还可以使用不同的颜色作为开始,中心和结束颜色.您附加的代码包含所有这些元素.

  • 谢谢你回答Karn,但有一件事我发现我只能在角度属性中给出45的倍数,除了它崩溃,即20,20,50等 (26认同)
  • @karn我真的很困惑你的回答.开始是逆时针方向从左到右.所以在图像1中,我希望箭头像这样移动 - > - > - > - >.也与'android:startColor ="#000000"`的方式相混淆.如果我们把起始角度设为90,是否意味着从角度90-270开始颜色会变黑?剩下的角度(0-90)怎么样? (6认同)
  • 起始颜色为黑色 (#000000) 似乎箭头朝相反方向,不是吗? (2认同)
  • @gikarasojo kinene 答案是正确的。除了,从三角学的角度来看,描述角度的向量从原点(坐标的开始)开始。角度是按逆时针方向测量的。所以当角度是 90(pi/2) 时,ort 向量看起来是向上的。因此,上述答案中的箭头指向错误。相反。第一张图也是一样。 (2认同)

Bur*_*lek 18

更简单地说,给出相对于您希望其开始的点的角度值。

在此输入图像描述

它将根据角度值从startColor开始。

90 的示例:

在此输入图像描述

270 的示例:

在此输入图像描述


Ale*_*lex 10

你可能想从代码中创建对角线渐变.它更容易,你有很多选择.这个片段对我有所帮助

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

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

然后从片段中的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;
    }
Run Code Online (Sandbox Code Playgroud)


pas*_*whu 9

指定形状的渐变颜色.属性:

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

似乎文档中的描述与karn的答案相矛盾?

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

  • 你让我的年龄必须是45的倍数 (2认同)