2013-01-17 155 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]。如果爲零,則沿梯度線均勻分佈顏色 。

(未測試,但它應該爲你做的伎倆)