To simply answer your question: yes, you do need to rerun the onDraw
process with invalidate()
.
Here's how I'd do it:
Create an ArrayList of
Paint
s. You'll need to be able to assign one to each rectangle so you can draw the rectangles with the correct color. Add however many default paint colors you might need. So if the user can only create up to 9 rectangles, add 9 Paints of the same initial color:for (int i = 0; i < MAX_RECTS; i++) { paintListName.add(new Paint()); paintListName.get(i).setColor(Color.BLACK); //Your default color }
Then, in the
onTouchEvent
method, change the color of the paint that is to be associated with the selected rectangle, then redraw:for (int c = 0; c < rectList.size(); c++) { if (rectList.get(c).contains(touchX, touchY)) { //Set it to color of your choice paintListName.get(c).setColor(Color.GREEN); invalidate(); } }
In
onDraw
, you should create each rectangle with its associated color:canvas.drawRect(rect, paintListName.get(c));