2012-10-26 20 views
1

我從android中搜索漸變到textView但沒有得到適當的解決方案。如何在Android中給TextView漸變?

我有這樣the text in the image is actually having the gradient

如果我把圖像作爲文本在屏幕上,但是我有許多的TextView其中具有梯度,然後應用可以是大型化。 所以如果有人知道解決方案,請發佈它。 謝謝。

回答

9

這裏有雲,

int[] color = {Color.DKGRAY,Color.CYAN}; 
     float[] position = {0, 1}; 
     TileMode tile_mode = TileMode.REPEAT; 
     LinearGradient lin_grad = new LinearGradient(0, 0, 0, 35,color,position, tile_mode); 
     Shader shader_gradient = lin_grad; 
     your_text_view.getPaint().setShader(shader_gradient); 

編輯:

嗨@android開發商,

如果你想要做一些垂直或視圖中的任何梯度,

請使用,

RadialGradient而不是LinearGradient.

唯一的一點是你必須調整,

x coordinate of the center of the radius. 
y coordinate of the center of the radius. 

and also, 

radius value of the view. 

和使用平鋪模式爲鏡,而不是重複。

我有樣,但它可能不準確的你想要什麼,

TileMode tile_mode = TileMode.MIRROR or TileMode.REPEAT; 
     RadialGradient rad_grad = new RadialGradient(0, 3, 5, Color.BLUE,Color.RED,tile_mode, tile_mode); 
     Shader shader_gradient = rad_grad; 
     your_text_view.getPaint().setShader(shader_gradient); 

你也可以使用同樣的SweepGradient代替LinearGradient

+0

感謝您的快速響應,但是當我將此漸變設置爲背景 –

+0

在何種意義上?...在​​xml佈局文件中,爲textview設置爲android:背景。 –

+0

對不起一半的評論,但是當我將此漸變設置爲背景時,它將應用於背景而不是textview顏色。 –