0

I'm using a custom View to draw rectangles(which will have text inside them at a certain point) and I want to highlight each rectangle when selected, which will happen every few seconds when user selects a rectangle. Should I implement highlighted rectangle in onDraw or is there a way just to redraw each rectangle without redrawing the whole View? I was thinking of using "invalidate(rect)" but it's been deprecated.

I'm trying to be considerate of the cost of invalidating the whole View compared to just redrawing a rectangle.

 protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for(CellCoordinates cellCoordinate : mCoordinateCells) {
            canvas.drawText(" ", cellCoordinate.getRect().exactCenterX(), cellCoordinate.getRect().exactCenterY(), cellPaint);
        }
    }
AtomicallyBeyond
  • 348
  • 1
  • 15
  • 1
    Does this answer your question? [Updating only a portion of View without using invalidate(Rect)](https://stackoverflow.com/questions/52647158/updating-only-a-portion-of-view-without-using-invalidaterect) – Bö macht Blau Dec 20 '20 at 21:07

1 Answers1

1

Using invalidate() and onDraw() is fine. Dirty rect is not really have effect on API21+

... In API 21 the given rectangle is ignored entirely in favor of an internally-calculated area instead. ...

Dirty rect is deprecated because of different drawing model in hardware accelerated views. Checkout this link for more information

Also, it seems your rectangles can be implemented as custom Drawables with states (selected and normal). It will not give you extra performance, but might help to divide and structure code for drawing. This might help

Mikhail
  • 309
  • 1
  • 6