2013-01-17 166 views
3

EDIT线性GradientDrawable的变化中心:看溶液上方机器人:通过代码

I M再用了。我只想设置一个线性GradientDrawable,它改变了渐变的垂直中心...绘制渐变效果很好,但我怎样才能改变它的中心?!?

RelativeLayout bgScreen = (RelativeLayout) findViewById(R.id.player_screen); 

GradientDrawable gd = new GradientDrawable(
    GradientDrawable.Orientation.TOP_BOTTOM, 
    new int[] {startColor,endColor}); 

gd.setCornerRadius(0f); 
gd.setAlpha(200); 
bgScreen.setBackground(gd); 


public void redrawOrChangeBackgroundGradient(){ 
    //??? either change center of existing 
    gd.setGradientCenter(float x, float y) //ONLY works in RADIAL_GRADIENT or SWEEP_GRADIENT. 

    //??? or complete redraw Gradient with different center 

} 

这里S的我多么想通过代码来改变渐变的图片例如

enter image description here

不能难,可以吗?

回答

2

缺少以编程方式设置中心为线性GradientDrawable s的能力已经是registered issue

但有一个解决方法described here。基本上,您应该从LinearGradient创建一个PaintDrawable,并将其设置为您的视图的背景可绘制。在此之后的解决方案,可以通过颜色映射到positions阵列设置在LinearGradient构造的中心:

浮法[]位置

可能为null。颜色数组中每个对应颜色 的相对位置[0..1]。如果为零,则沿梯度线均匀分布颜色 。

(未测试,但它应该为你做的伎俩)