1,gradient(渐变)
【1】<gradient>用以定义渐变色,可以定义两色渐变和三色渐变,及渐变样式;
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" > <gradient android:type=["linear" | "radial" | "sweep"] //共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变 android:angle="integer" //渐变角度,必须为45的倍数,0为从左到右,90为从上到下 android:centerx="float" //渐变中心x的相当位置,范围为0~1 android:centery="float" //渐变中心y的相当位置,范围为0~1 android:startcolor="color" //渐变开始点的颜色 android:centercolor="color" //渐变中间点的颜色,在开始与结束点之间 android:endcolor="color" //渐变结束点的颜色 android:gradientradius="float" //渐变的半径,只有当渐变类型为radial时才能使用 android:uselevel=["true" | "false"] /> //使用levellistdrawable时就要设置为true。设为false时才有渐变效果
首先有三种渐变类型,分别是:linear(线性渐变)、radial(放射性渐变)、sweep(扫描式渐变)
</shape>
2,demo实现效果
【1】 线性渐变
实现效果
shape 代码
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <gradient android:type="linear" android:startcolor="#ff0000" android:centercolor="#00ff00" android:endcolor="#0000ff"/> </shape>
【2】 放射性渐变
实现效果
实现代码
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <gradient android:type="radial" android:startcolor="#ff0000" android:centercolor="#00ff00" android:endcolor="#0000ff" android:gradientradius="100"/> </shape>
【3】 扫描式渐变
实现效果
实现代码
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <gradient android:type="sweep" android:startcolor="#ff0000" android:centercolor="#00ff00" android:endcolor="#0000ff"/> </shape>
【4】android:angle属性修改渐变角度 (仅对线性渐变有效)
1)android:angle="integer" //渐变角度,必须为45的倍数,0为从左到右,90为从上到下
2)angle属性确实只对线性渐变有效,其它两种渐变方式都没有任何动静
实现效果:
实现代码:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <gradient android:type="linear" android:startcolor="#ff0000" android:centercolor="#00ff00" android:endcolor="#0000ff" android:angle="45"/> </shape>
【5】android:centerx与android:centery
1) android:centerx="0.2",android:centery="0.8"
2)centerx、centery两个属性用于设置渐变的中心点位置,仅当渐变类型为放射渐变时有效。
3)类型为分数或小数,不接受dimension。默认值是0.5,有效值是0.0~1.0,超出该范围后会看不出渐变效果。centerx、centery的取值其实是宽和高的百分比
实现效果
实现代码: 取宽度的20%和高度的80%的位置,作为新的渐变原点
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <gradient android:type="sweep" android:startcolor="#ff0000" android:centercolor="#00ff00" android:endcolor="#0000ff" android:centerx="0.2" android:centery="0.8"/> </shape>
总结
以上所述是小编给大家介绍的android实现 shape属性gradient 渐变效果,希望对大家有所帮助
如对本文有疑问, 点击进行留言回复!!
Codeforces C. A Cookie for You (模拟 / 分类 / 贪心) (Round #654 Div.2)
DialogFragment弹窗(带黑色透明背景和不带黑色透明背景)
网友评论