android渐变中的角度属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12153890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:54:12  来源:igfitidea点击:

angle attribute in android gradient

androidandroid-layoutgradientandroid-shape

提问by Sharanabasu Angadi

I am going through test example. Where for some Image background they are using gradient, the code goes like this

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

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

In the above xml I didn't get angleattribute. but when I change the value of angleslightly the pattern slants. Can anyone explain me how exactly it works?

在上面的 xml 中我没有得到angle属性。但是当我angle稍微改变图案倾斜的值时。谁能解释一下它到底是如何工作的?

回答by karn

Gradient basically represents the variation in space(in a direction) of any quantity. With color it represents the variation of color intensity in a direction represented by angle. Here are some diagrams to represent this concept:
enter image description here

梯度基本上表示任何数量的空间(方向)变化。用颜色表示颜色强度在角度表示的方向上的变化。下面是一些图表来表示这个概念:
在此处输入图片说明

Here the figure shows the color variation in horizontal direction (angle is set 0).
XML code:

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

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

enter image description here

在此处输入图片说明

Here the figure shows the color variation in vertical direction (angle is set 90).
XML code:

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

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

You can also use different color as start, center and end colors. The code you attached contains all these elements.

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

回答by passerbywhu

Specifies a gradient color for the shape. attributes:

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

android:angle Integer. The angle for the gradient, in degrees. 0 is left to right, 90 is bottom to top. It must be a multiple of 45. Default is 0.

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

It seems that the description in the documentation contradict to karn's answer??

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

You can find more details in the documentation

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

回答by Alex

you might wanna create diagonal gradient from code. It's much easier and you have a lot of options open from there. This snippet helped me

您可能想从代码创建对角渐变。这要容易得多,而且您可以从那里打开很多选项。这个片段帮助了我

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

available directions from GradientDrawable class

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

and you call the method from onCreate or onCreateView in fragment and pass parent view(in my case).

并且您从 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;
    }