我正在看测试用例。对于一些图像背景,他们使用渐变,代码是这样的
<?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的值时,模式就会倾斜。有人能给我解释一下它是怎么工作的吗?
发布于 2012-08-28 16:35:49
梯度基本上表示任何量在空间(在一个方向上)的变化。对于颜色,它表示颜色强度在由角度表示的方向上的变化。下面是一些用来表示这个概念的图表:

该图显示了水平方向上的颜色变化(角度设置为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>您还可以使用不同的颜色作为开始颜色、中心颜色和结束颜色。您附加的代码包含所有这些元素。
发布于 2015-06-11 11:20:00
指定形状的渐变颜色。属性:
android:angle。渐变的角度,单位为度。0表示从左到右,90表示从下到上。它必须是45的倍数。默认值为0。
文档中的描述似乎与卡恩的答案相矛盾??
您可以在documentation中找到更多详细信息
发布于 2016-07-07 02:30:09
你可能想从代码中创建对角线渐变。这要容易得多,而且你有很多选择。这段代码帮助了我
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;
}https://stackoverflow.com/questions/12153890
复制相似问题