2011-03-08 26 views
0

有沒有辦法將漸變放入android 2.1中的位圖對象?圖像必須是這樣的:android 2.1中的位圖漸變

image with gradient

我需要的梯度只對位圖的頂部。 DrawableGradient或LinearGradient僅來自android 2.2,所以這些對象根本沒有幫助我。謝謝

回答

3

你需要從XML或從代碼?在代碼中,試試這個:

/* Create a 200 x 200 bitmap and fill it with black. */ 
    Bitmap b = Bitmap.createBitmap(200, 200, Config.ARGB_8888); 
    Canvas c = new Canvas(b); 
    c.drawColor(Color.BLACK); 

    /* Create your gradient. */ 
    LinearGradient grad = new LinearGradient(0, 0, 0, 50, Color.GRAY, Color.BLACK, TileMode.CLAMP); 

    /* Draw your gradient to the top of your bitmap. */ 
    Paint p = new Paint(); 
    p.setStyle(Style.FILL); 
    p.setShader(grad); 
    c.drawRect(0, 0, 200, 50, p); 

在XML中,只需在垂直線性佈局中創建兩個單獨的視圖。頂視圖應該有一個漸變的可繪製背景,底部較高的視圖應該有一個堅實的背景。

+0

但我的圖像可能是紅色或藍色的東西...它並不總是相同的圖像... – 2011-03-09 06:45:03

+0

爲什麼你的圖像顏色很重要?你不只是把它疊加在漸變的頂部嗎? – Josh 2011-03-09 15:17:39

+0

我有同樣的問題,事情是梯度應該是圖像,而不是在它之上的黑色/白色疊加來隱藏圖像內容。我想要位圖實際上在一側是透明的,而在另一側是不透明的。 – 2014-07-10 15:18:56