Android怎么实现形状属性梯度渐变效果

  介绍

这篇文章主要介绍Android怎么实现形状属性梯度渐变效果,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

<强> 1,梯度(渐变)

【1】& lt; gradient>用以定义渐变色,可以定义两色渐变和三色渐变,及渐变样式;,

& lt; ? xml  version=?.0“,编码=皍tf-8" ?祝辞& lt; shape  xmlns: Android=癶ttp://schemas.android.com/apk/res/android",在   & lt;梯度   ,,,android: type=[“linear", |,“radial", |,“sweep"),,,,//共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变,,   ,,,android:角=癷nteger",,,,,//渐变角度,必须为45的倍数,0为从左到右,90为从上到下,,   ,,,android: centerX=癴loat",,,,,//渐变中X的心相当位置,范围为0 ~ 1,,   ,,,android: centerY=癴loat",,,,,//渐变中心Y的相当位置,范围为0 ~ 1,,   ,,,android: startColor=癱olor",,,//渐变开始点的颜色,,   ,,,android: centerColor=癱olor",,//渐变中间点的颜色,在开始与结束点之间,,   ,,,android: endColor=癱olor",,,,//渐变结束点的颜色,,   ,,,android: gradientRadius=癴loat",,//渐变的半径,只有当渐变类型为径向时才能使用,,   ,,,android: useLevel=[“true", |,“false"],/祝辞,,//使用LevelListDrawable时就要设置为真的。设为false时才有渐变效果

首先有三种渐变类型,分别是:线性(线性渐变),径向(放射性渐变),扫描(扫描式渐变)

& lt;/shape>

<李>

android: useLevel属性通常不使用。该属性用于指定是否将该形状当成一个LevelListDrawable来使用,默认值为假的。

<李>

角属性确实只对线性渐变有效,其它两种渐变方式都没有任何动静

<李>

centerX, centerY两个属性用于设置渐变的中心点位置,仅当渐变类型为放射渐变时有效只

<强> 2,演示实现效果,

<李>

我们使用三色渐变来看看这三种渐变方式都是怎么显示的:(如果不使用centerColor属性就是双色渐变,这个属性是可选的)

<李>

注意:在构造放射性渐变时,要加上android: gradientRadius属性(渐变半径),即必须指定渐变半径的大小才会起作用

【1】线性渐变

实现效果

 android怎么实现形状属性梯度渐变效果

shape 代码

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   & lt; shape  xmlns: android=癶ttp://schemas.android.com/apk/res/android",在   ,& lt;梯度   ,,,android: type=發inear"   ,,,android: startColor=? ff0000"   ,,,android: centerColor=? 00 ff00"   ,,,android: endColor=? 0000 ff"/比;   & lt;/shape>

【2】放射性渐变

实现效果,

 Android怎么实现形状属性梯度渐变效果

实现代码

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   & lt; shape  xmlns: android=癶ttp://schemas.android.com/apk/res/android",在   ,& lt;梯度   ,,,android: type=皉adial"   ,,,android: startColor=? ff0000"   ,,,android: centerColor=? 00 ff00"   ,,,android: endColor=? 0000 ff"   ,,,android: gradientRadius=?00“/比;   & lt;/shape>

【3】扫描式渐变

实现效果

 Android怎么实现形状属性梯度渐变效果

实现代码

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   & lt; shape  xmlns: android=癶ttp://schemas.android.com/apk/res/android",在   ,& lt;梯度   ,,,android: type=皊weep"   ,,,android: startColor=? ff0000"   ,,,android: centerColor=? 00 ff00"   ,,,android: endColor=? 0000 ff"/比;   & lt;/shape>

【4】android:角属性修改渐变角度(仅对线性渐变有效)

,,,1)android:角=癷nteger"//渐变角度,必须为45的倍数,0为从左到右,90为从上到下

,,,2)角属性确实只对线性渐变有效,其它两种渐变方式都没有任何动静,

实现效果:

 Android怎么实现形状属性梯度渐变效果”>,</p> <p>实现代码:</p> <pre类= & lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   & lt; shape  xmlns: android=癶ttp://schemas.android.com/apk/res/android",在   ,& lt;梯度   ,,,android: type=發inear"   ,,,android: startColor=? ff0000"   ,,,android: centerColor=? 00 ff00"   ,,,android: endColor=? 0000 ff"   ,,,android:角=?5“/比;   null   null   null   null   null   null   null   null   null   null

Android怎么实现形状属性梯度渐变效果