2

我正在尝试更改触摸部分中图像的颜色。我尝试了以下两种方法,

1)

  mPaint = new Paint();  
  mPaint.setColor(Color.RED);  
  mPaint.setStyle( Paint.Style.STROKE );  
  mPaint.setStrokeJoin( Paint.Join.ROUND );  
  mPaint.setStrokeCap( Paint.Cap.ROUND );  
  mPaint.setStrokeWidth( SettingsActivity.brushsize + 5);  
  mPaint.setFilterBitmap( false );  

结果:在这种情况下混合没有发生,颜色完全覆盖图像。

2)

  mPaint = new Paint();  
  ColorFilter filter = new LightingColorFilter(Color.RED, 1);  
  mPaint.setColorFilter(filter);  
  mPaint.setStyle( Paint.Style.STROKE );  
  mPaint.setStrokeJoin( Paint.Join.ROUND );  
  mPaint.setStrokeCap( Paint.Cap.ROUND );  
  mPaint.setStrokeWidth(  5);  
  mPaint.setFilterBitmap( false );  

结果:我总是用这个观察黑色。

如何在图像的触摸部分执行颜色混合?

4

1 回答 1

1

Use Flood Fill Algorithm. Check out this code :

private void FloodFill(Bitmap bmp, Point pt, int targetColor, int replacementColor){
Queue<Point> q = new LinkedList<Point>();
q.add(pt);
while (q.size() > 0) {
    Point n = q.poll();
    if (bmp.getPixel(n.x, n.y) != targetColor)
        continue;

    Point w = n, e = new Point(n.x + 1, n.y);
    while ((w.x > 0) && (bmp.getPixel(w.x, w.y) == targetColor)) {
        bmp.setPixel(w.x, w.y, replacementColor);
        if ((w.y > 0) && (bmp.getPixel(w.x, w.y - 1) == targetColor))
            q.add(new Point(w.x, w.y - 1));
        if ((w.y < bmp.getHeight() - 1)
                && (bmp.getPixel(w.x, w.y + 1) == targetColor))
            q.add(new Point(w.x, w.y + 1));
        w.x--;
    }
    while ((e.x < bmp.getWidth() - 1)
            && (bmp.getPixel(e.x, e.y) == targetColor)) {
        bmp.setPixel(e.x, e.y, replacementColor);

        if ((e.y > 0) && (bmp.getPixel(e.x, e.y - 1) == targetColor))
            q.add(new Point(e.x, e.y - 1));
        if ((e.y < bmp.getHeight() - 1)
                && (bmp.getPixel(e.x, e.y + 1) == targetColor))
            q.add(new Point(e.x, e.y + 1));
        e.x++;
    }
}}
于 2012-09-22T05:44:54.767 回答