1

我是 Android 2D 图形的新手,我想知道是否可以这样做:

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

使用上面链接中的图像,我想根据我提供的角度用特定颜色填充圆圈的白色部分,将黑色和透明部分保持原样。

我设法使用该drawArc()方法制作了一条弧线,但它覆盖了图像。由于图像中的弧线不是一个完美的圆,它被稍微压扁了,这个问题变得复杂了。

有没有办法只在空白处绘制?使用过滤器或面具?如果您有示例代码,我可以使用它会很棒!:)

谢谢

4

2 回答 2

3

尝试这个

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;
}

通过使用此代码,我成功地在非透明区域填充颜色并保持透明区域不变。

你也可以这样检查:

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

您可以根据需要比较任何颜色 Color.BLUE 任何应用其他方法。

于 2013-11-26T17:53:16.937 回答
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);
于 2013-03-15T22:16:24.930 回答