2013-03-15 55 views
0

我是新來Android的2D圖形,我想知道是否有可能做到這一點:Android:如何繪製一個位圖的單一顏色?

https://dl.dropbox.com/u/6818591/pendulum_background.png

在上面的鏈接使用的形象,我想填補的白色部分根據我提供的角度以特定顏色進行圓圈,使黑色和透明部分保持原樣。

我設法做了一個使用drawArc()方法的弧,但它覆蓋了圖像。問題很複雜,因爲圖像中的弧線不是一個完美的圓形,而是被微微壓扁。

有沒有隻能在白色空間上繪製的方法?使用過濾器或面具?如果您有示例代碼,我可以使用它,太棒了! :)

感謝

回答

1

您可以在使用位圖來canvas.drawPaint(..)上畫一個特定的顏色與另一個。

// make a mutable copy and a canvas from this mutable bitmap 
Bitmap bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); 
Canvas canvas = new Canvas(bitmap); 

// get the int for the colour which needs to be removed 
Paint paint = new Paint(); 
paint.setARGB(255, 0, 0, 0); // ARGB for the color, in this example, white 
int removeColor = paint.getColor(); // store this color's int for later use 

// Next, set the color of the paint to the color another color    
paint.setARGB(/*put ARGB values for color you want to change to here*/); 

// then, set the Xfermode of the pain to AvoidXfermode 
// removeColor is the color that will be replaced with the paint color 
// 0 is the tolerance (in this case, only the color to be removed is targetted) 
// Mode.TARGET means pixels with color the same as removeColor are drawn on 
paint.setXfermode(new AvoidXfermode(removeColor, 0, AvoidXfermode.Mode.TARGET)); 

// re-draw 
canvas.drawPaint(p); 
+0

謝謝你的回答@詹姆斯,但我真的不能真正把它工作。這是我在我的onDraw方法中的代碼: 位圖bitmap = bit.copy(Bitmap.Config.ARGB_8888,true); \t \t Paint paint = new Paint(); \t \t paint.setARGB(255,0,0,0); // ARGB爲顏色,在本例中爲白色 \t \t int removeColor = paint.getColor(); \t \t paint.setARGB(255,255,0,0); (新的AvoidXfermode(removeColor,0,AvoidXfermode.Mode.TARGET)); \t \t canvas.drawBitmap(bitmap,0,0,null); \t \t canvas.drawPaint(paint); 我得到的只是一個紅色的屏幕。 – RadicalMonkey 2013-03-19 13:21:59

+0

您需要修改'paint.setARGB(255,0,0,0)'行以匹配要覆蓋的顏色的ARGB值。 – 2013-03-19 17:25:28

2
嘗試

private Drawable fillBitmap(Bitmap bitimg1, int r, int g, int b) { 
     Bitmap bitimg = bitimg1.copy(bitimg1.getConfig(), true); 


    int a = transperentframe; 
    Drawable dr = null; 
    for (int x = 0; x < bitimg.getWidth(); x++) { 
     for (int y = 0; y < bitimg.getHeight(); y++) { 

      int pixelColor = bitimg.getPixel(x, y); 
      int A = Color.alpha(pixelColor); 
      bitimg.setPixel(x, y, Color.argb(A, r, g, b)); 
     } 
    } 
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitimg, 
      framewidth + 10, frameheight, true); 

    dr = new BitmapDrawable(getResources(), resizedBitmap); 

    return dr; 
} 

通過使用該代碼i的非透明區域填充顏色成功離開了透明區域,因爲它是。

ü還可以檢查這樣的:

if(canvasBitmap.getPixel(x, y) == Color.TRANSPARENT) 

你可以比較任意顏色Color.BLUE任何根據您的需要應用其他方法。

+0

Akanksha:請遵循以下網址:http://stackoverflow.com/questions/20697189/fill-color-on-bitmap-in-android/20699644?noredirect=1#comment31008587_20699644 ...我想整合以填充顏色使用android圖形的圖像。可能嗎 – Hardik 2013-12-23 06:22:18

相關問題