Sha*_*adi 66 android gradient android-layout android-shape
我正在通过测试示例.对于某些图像背景,他们使用渐变,代码就像这样
<?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>
Run Code Online (Sandbox Code Playgroud)
在上面的xml我没有得到angle属性.但是当我angle稍微改变模式的值时,模式倾斜.谁能解释我究竟是如何运作的?
kar*_*arn 154
梯度基本上代表任何数量的空间(在一个方向上)的变化.对于颜色,它表示颜色强度在由角度表示的方向上的变化.以下是一些代表这一概念的图表:

此处图中显示了水平方向的颜色变化(角度设置为0).
XML代码:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#000000"
android:angle="0"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

这里的图显示了垂直方向的颜色变化(角度设置为90).
XML代码:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#000000"
android:angle="90"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
您还可以使用不同的颜色作为开始,中心和结束颜色.您附加的代码包含所有这些元素.
Ale*_*lex 10
你可能想从代码中创建对角线渐变.它更容易,你有很多选择.这个片段对我有所帮助
public void SetGradient(View view) {
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TL_BR,
new int[]{0xFF141a24, 0xFF293f49, 0xFF72554c});
view.setBackground(gd);
}
Run Code Online (Sandbox Code Playgroud)
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,
}*/
Run Code Online (Sandbox Code Playgroud)
然后从片段中的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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54258 次 |
| 最近记录: |