阅读量:111
Android GradientDrawable 支持以下渐变属性:
-
角度(
angle):设置渐变的方向。值是相对于正x轴的角度,0度表示从左到右,90度表示从上到下,180度表示从右到左,270度表示从下到上。<shape android:shape="rectangle"> <gradient android:angle="90" android:startColor="#f06" android:endColor="#ff0" /> </shape> -
类型(
type):设置渐变的类型。可选值有linear(线性渐变)、radial(径向渐变)和sweep(扫描渐变)。<shape android:shape="rectangle"> <gradient android:type="linear" android:startColor="#f06" android:endColor="#ff0" /> </shape> -
中心点(
centerX和centerY):设置渐变的中心点。这些值是相对于形状的宽度和高度的百分比。<shape android:shape="rectangle"> <gradient android:type="radial" android:centerX="0.5" android:centerY="0.5" android:startColor="#f06" android:endColor="#ff0" /> </shape> -
半径(
radius):设置径向渐变的半径。值是相对于形状的宽度和高度的百分比。<shape android:shape="rectangle"> <gradient android:type="radial" android:centerX="0.5" android:centerY="0.5" android:radius="0.5" android:startColor="#f06" android:endColor="#ff0" /> </shape> -
使用角度(
useAngle):设置是否使用角度来定义渐变的方向。如果设置为true,则angle属性将生效;如果设置为false,则渐变方向将根据形状的边界来确定。<shape android:shape="rectangle"> <gradient android:type="linear" android:useAngle="true" android:angle="45" android:startColor="#f06" android:endColor="#ff0" /> </shape> -
使用中心点(
useCenter):设置是否使用中心点来定义渐变的方向。如果设置为true,则centerX和centerY属性将生效;如果设置为false,则渐变方向将根据形状的边界来确定。<shape android:shape="rectangle"> <gradient android:type="radial" android:useCenter="true" android:centerX="0.5" android:centerY="0.5" android:startColor="#f06" android:endColor="#ff0" /> </shape> -
渐变颜色(
startColor、centerColor、endColor):设置渐变的起始颜色、中心颜色和结束颜色。<shape android:shape="rectangle"> <gradient android:type="linear" android:angle="0" android:startColor="#f06" android:centerColor="#ff0" android:endColor="#0f0" /> </shape> -
渐变透明度(
startAlpha、centerAlpha、endAlpha):设置渐变的起始透明度、中心透明度和结束透明度。这些值的范围是 0(完全透明)到 255(完全不透明)。<shape android:shape="rectangle"> <gradient android:type="linear" android:angle="0" android:startColor="#f0600000" android:centerColor="#ff000000" android:endColor="#0f000000" /> </shape>