在android中的循环渐变
我试图做一个渐变,从屏幕中间发出的白色,并转向黑色,因为它向屏幕边缘移动。
当我做出这样的“正常”渐变时,我一直在尝试不同的形状:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="#E9E9E9" android:endColor="#D4D4D4" android:angle="270"/> </shape> 当使用“椭圆形”形状时,我至less有一个圆形,但没有梯度效果。 我怎么能做到这一点?
 你可以使用android:type="radial"获得一个循环渐变android:type="radial" : 
 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:type="radial" android:gradientRadius="250" android:startColor="#E9E9E9" android:endColor="#D4D4D4" /> </shape> 
我总是在学习新概念时发现图像有帮助,所以这是一个补充答案。
  
 
  %p表示父级的百分比,即我们设置的任何视图的最窄维度的百分比。 上面的图片是通过改变这个代码中的gradientRadius生成的 
my_gradient_drawable
 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:type="radial" android:gradientRadius="10%p" android:startColor="#f6ee19" android:endColor="#115ede" /> </shape> 
 可以像这样设置视图的background属性 
 <View android:layout_width="200dp" android:layout_height="100dp" android:background="@drawable/my_gradient_drawable"/> 
中央
你可以改变半径的中心
 android:centerX="0.2" android:centerY="0.7" 
 其中小数分别是x和y的宽度和高度的分数。 
  
 
我想你应该添加android:centerColor
 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="#FFFFFF" android:centerColor="#000000" android:endColor="#FFFFFF" android:angle="0" /> </shape> 
此示例显示从白色到黑色到白色的水平渐变。
 <gradient android:centerColor="#c1c1c1" android:endColor="#4f4f4f" android:gradientRadius="400" android:startColor="#c1c1c1" android:type="radial" > </gradient>