I have two classes: CustomView extends View and MainActivity extends Activity. In CustomView, I draw a series of rounded squares (canvas.drawRoundRect) using a loop . I know how to detect clicks on any given square but I don't know how to change a square's color. How do I call the onDraw method from MainActivity? Or if there is an update method I can use to invalidate() from the MainActivity class. Bottom line is I want to know how to change my shape's color whenever I click on it. Thank you.
Android: I have many shapes drawn. I need to change a shape's color whenever I click on it
99 views Asked by AudioBubble At
3
There are 3 answers
0
On
For this you need to get color of clicked pixel and then use below Flood-fill algorithm and pass your bitmap , Point which you have clicked on Bitmap , Target and replacement Color 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++;
}
}
}
you may search more about flood fill algorithm for batter understanding .
https://github.com/latemic/ColorBooth/blob/master/src/com/colorbooth/FloodFill.java
In your onDraw() method draw your rectangles by setting paint color in a varible