如何使用 Android GradientDrawable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2666341/
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
How to use Android GradientDrawable
提问by Janusz
I try to use the GradientDrawable to set a gradient to some backgrounds and buttons. Sadly the documentationis not very detailed.
我尝试使用 GradientDrawable 为某些背景和按钮设置渐变。遗憾的是,文档不是很详细。
What are the main attributes to configure the gradient? I understand start and endcolor but some of the other attributes could need some explanation.
配置渐变的主要属性是什么?我理解 start 和 endcolor,但其他一些属性可能需要一些解释。
At the moment I used images as the background for the buttons but a drawable defined in XML would be much nicer.
目前我使用图像作为按钮的背景,但在 XML 中定义的 drawable 会更好。
I try to get a look like this (It is a very light gradient): alt text http://janusz.de/~janusz/RedButton.png
我试着看起来像这样(这是一个非常轻的渐变):alt text http://janusz.de/~janusz/RedButton.png
回答by Praveen
use this xml as background to the imageview.
使用此 xml 作为 imageview 的背景。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="90" android:startColor="#7c0000" android:endColor="#A71C1C"/>
</shape>
thats it.
就是这样。
回答by Dandre Allison
I will give the same answer as Praveen, but will try to explain the settings as well.
我将给出与Praveen相同的答案,但也会尝试解释设置。
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="-90"
android:startColor="#7c0000"
android:endColor="#A71C1C" />
</shape>
android:type
机器人:类型
There are 3 types of gradients, the default and the one for this question is "linear". The other 2 are "radial" and "sweep".
有 3 种类型的渐变,默认和此问题的一种是“线性”。另外两个是“径向”和“扫描”。
android:angle
机器人:角度
Counter-clockwise rotation of the gradient, where 0 is | start color --> end color | (horizontally).
逆时针旋转渐变,其中 0 是 | 开始颜色 --> 结束颜色 | (水平)。
android:startColor
机器人:开始颜色
Color the gradient starts from, start is defined by the rotation.
渐变开始的颜色,开始由旋转定义。
android:endColor
机器人:结束颜色
Color the gradient ends with, end is defined by the rotation.
渐变结束的颜色,结束由旋转定义。
android:centerColor
机器人:中心颜色
There can also be a color in between the start and end color, if desired.
如果需要,也可以在开始颜色和结束颜色之间添加一种颜色。
回答by Suragch
Code
代码
I originally found this question because I wanted to do it in code. Here is how to do it programmatically.
我最初发现这个问题是因为我想用代码来做。以下是如何以编程方式执行此操作。
int startColor = 0xfff6ee19; // yellow
int endColor = 0xff115ede; // blue
GradientDrawable gradientDrawable = new GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT,
new int[] {startColor, endColor});
View myView = findViewById(R.id.my_view);
myView.setBackgroundDrawable(gradientDrawable);
The different orientations in the image at the top can be achieved by changing the Orientation
in the constructor.
顶部图像中的不同方向可以通过更改Orientation
构造函数中的来实现。
XML
XML
As already answered, this is how you do it in xml.
正如已经回答的那样,这就是您在 xml 中的做法。
my_gradient_drawable.xml:
my_gradient_drawable.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="0"
android:startColor="#f6ee19"
android:endColor="#115ede" />
</shape>
You set it to the background of some view. For example:
您将其设置为某个视图的背景。例如:
<View
android:layout_width="200dp"
android:layout_height="100dp"
android:background="@drawable/my_gradient_drawable"/>