【问题标题】:angle attribute in android gradientandroid渐变中的角度属性
【发布时间】:2012-08-28 06:32:34
【问题描述】:

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

<?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 的值时,模式会倾斜。谁能解释一下它是如何工作的?

【问题讨论】:

    标签: android android-layout gradient android-shape


    【解决方案1】:

    梯度基本上表示任何量的空间(方向)变化。用颜色表示颜色强度在以角度表示的方向上的变化。这里有一些图表来表示这个概念:

    下图是水平方向的颜色变化(角度设置为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>
    

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

    【讨论】:

    • 感谢卡恩的回答,但我发现我只能在角度属性中给出 45 的倍数,除了它会崩溃,即 20、20、50 等
    • 渐变的表达方式有很多种。这里我们实际上使用的是线性梯度(沿直线斜率 m=(y-y1)/(x-x1))。当它是 45 的倍数时,x 和 y 的变化是相同的。可能是这个原因的原因。我知道的不多。
    • 起始颜色为黑色 (#000000) 似乎箭头朝相反方向前进,不是吗?
    • @karn 对您在这里的回答感到非常困惑。开始是从左到右逆时针方向。所以在图 1 中,我希望箭头像这样移动 ->->->-> 。也对android:startColor="#000000" 的工作方式感到困惑。如果我们将起始角度设为 90,是否意味着从 90-270 角度颜色将变为黑色?剩下的角度(0-90)呢?
    • @gikarasojo kinene 答案是正确的。除了,从三角学的角度来看,描述角度的矢量从原点(坐标的起点)开始。以逆时针方向测量角度。因此,当角度为 90(pi/2) 时,ort 向量向上查找。因此,上述答案中的箭头指向错误。相反。第一张照片也是如此。
    【解决方案2】:

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

    机器人:角度 整数。渐变的角度,以度为单位。 0 是从左到右,90 是从下到上。它必须是 45 的倍数。默认为 0。

    文档中的描述似乎与卡恩的回答相矛盾??

    您可以在documentation找到更多详细信息

    【讨论】:

    • 你让我在must be a multiple of 45
    • 是的,有没有办法绕过这个 45 度角规则而不使用 SWEEP_GRADIENT?
    • @Mr.Drew 您可以使用 LinearGradient 的前 4 个参数创建不同的角度,如link 中所述。之后使用linearGradient.setLocalMartix() 将其缩放到所需的大小。
    • 对于 Android official doc
    【解决方案3】:

    您可能想从代码中创建对角渐变。这要容易得多,并且您可以从那里打开很多选项。这个sn-p帮了我

    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,
        }*/
    

    然后您从片段中的 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;
        }
    

    【讨论】:

      【解决方案4】:

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

      它将根据角度值以startColor开头。

      90 的示例:

      270 的示例:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-28
        • 2018-04-17
        • 1970-01-01
        • 2014-04-09
        相关资源
        最近更新 更多