我试着以编程方式复制下面的梯度。
<形状的xmlns:机器人=http://schemas.android.com/apk/res/android>
<梯度
机器人:startColor =@色/ startcolor
机器人:centerColor =#343434
机器人:endColor =#00000000
机器人:TYPE =放射状
机器人:gradientRadius =140
机器人:centerY =45%
/>
<边角机器人:半径=0dp/>
< /形状>
如何设置编程的paramether?谢谢
安卓centerY =45%
解决方案
http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html
要设置特定参数(我假设你没有指定一个的centerX值):
yourGradientDrawable.setGradientCenter(1.0F,0.45F);
因此,为了创建上述梯度(除了用不同的颜色)编程:
GradientDrawable G =新GradientDrawable(Orientation.TL_BR,新INT [] {getResources()的getColor(R.color.startcolor),Color.rgb(255,0,0) ,Color.BLUE});
g.setGradientType(GradientDrawable.RADIAL_GRADIENT);
g.setGradientRadius(140.0f);
g.setGradientCenter(0.0,0.45F);
注:定向忽略径向渐变,但需要注意那些在颜色的构造
Im trying to reproduce the following gradient programmatically.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="@color/startcolor"
android:centerColor="#343434"
android:endColor="#00000000"
android:type="radial"
android:gradientRadius="140"
android:centerY="45%"
/>
<corners android:radius="0dp" />
</shape>
How can I set programmatically the paramether? Thanks
android:centerY="45%"
解决方案
http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html
To set that specific parameter (I'm assuming a centerX value as you haven't specified one):
yourGradientDrawable.setGradientCenter(1.0f, 0.45f);
So to create the above gradient (except with different colors) programatically:
GradientDrawable g = new GradientDrawable(Orientation.TL_BR, new int[] { getResources().getColor(R.color.startcolor), Color.rgb(255, 0, 0), Color.BLUE });
g.setGradientType(GradientDrawable.RADIAL_GRADIENT);
g.setGradientRadius(140.0f);
g.setGradientCenter(0.0f, 0.45f);
Note: The orientation is ignored for a radial gradient but is needed for the constructor that takes colors.