Android: I have many shapes drawn. I need to change a shape's color whenever I click on it

99 views Asked by At

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.

3

There are 3 answers

0
Jinesh Francis On

In your onDraw() method draw your rectangles by setting paint color in a varible

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    firstPaint.setColor(firstRectColor);
    canvas.drawRoundRect(//..,firstPaint);
    //..
    setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if(motionEvent.getAction()==MotionEvent.ACTION_DOWN){
               if(((motionEvent.getX()>=firstRectX) && (motionEvent.getX()<=firstRectX+firstRectWidth))&&((motionEvent.getY()>=firstRectY) && (motionEvent.getY()<=firstRectY+firstRectHeight))){
                //touch point is inside first rectangle
                //assign the color to firstRectColor variable and call invalidate to redraw
                firstRectColor=getColorToChange();
                invalidate();
               }//..else if(){}
            } 
            return true;
        }
    });
}
0
Abraham.P On

Use the following: once you have named and declared their forms in Java code, you can change it as follows call the object by its name and place the following:

"Name of the object" .setbackgroundColor ("Name of the object" .getContext ().GetResources (). GetColor (R.color. "Desired color")
0
Chetan Joshi 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