2013-03-15 48 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

相关问题