-->

在机器人梯度角属性(angle attribute in android gradient)

2019-06-27 11:50发布

我会通过测试的例子。 其中一些图片的背景,他们所使用的梯度,代码是这样的

<?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稍微图案斜面。 谁能给我解释一下它究竟是如何工作的?

Answer 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>

您还可以使用不同的颜色作为开始,中心和边缘的颜色。 您附上的代码包含了所有这些元素。



Answer 2:

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

安卓:角整型。 渐变的角度,单位为度。 0被左到右,图90是底部至顶部。 它必须是45默认的倍数为0。

看来,文档中的描述矛盾到卡恩的回答?

你可以找到更详细的文档



Answer 3:

你可能想创建代码对角线梯度。 这是很容易,你有很多选择从那里打开。 这段代码帮助我

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;
    }


文章来源: angle attribute in android gradient