Android GradientDrawable 支持以下漸變屬性:
角度(angle):設置漸變的方向。值是相對于正x軸的角度,0度表示從左到右,90度表示從上到下,180度表示從右到左,270度表示從下到上。
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:startColor="#f06"
android:endColor="#ff0" />
</shape>
類型(type):設置漸變的類型??蛇x值有 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>