Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does ImageView just redraw part of its content when invalidate(Rect) is called?

I am new to Android development, just reading docs and trying the APIs. I am quit confused how ImageView managed to draw just a part of its content after an invalidate(Rect) invocation.

I've checked ImageView.java, found no other drawing method except onDraw(Canvas), but onDraw(Canvas) only cut the drawable only if it is beyound the view's visible boundary. I also read the implementation of View.invalidate(Rect), I think the key of this function is calling to mParent.invalidateChild(this, r); However, I think the parent view doesn't know how to draw the child in the given Rect, it finally has to call some method of it child to paint out.

Has anybody investigated this part of codes? Would you please give me some guide?

like image 209
imax366 Avatar asked Apr 06 '10 02:04

imax366


2 Answers

As far as I can gather there are two optimizations going one. For one, if the child is a viewgroup, only those children of this group are redrawn that intersect the invalidated area. Also, the canvas is clipped to the bounds of this rect. This means that less pixels have to be shuffled through the bus to the framebuffer, and that draw operations that are clipped completely can be skipped.

This is a semi-educated guess. I browsed the source a few months back and am fairly certain of the ViewGroup thing. The second one can be tested by overriding clipping in onDraw and checking if it redraws everything.

like image 62
Kevin Read Avatar answered Sep 30 '22 05:09

Kevin Read


I guess that code is in the View class.

There are some two interesting answers from Romain Guy in the google group.

  • Incorrect clip rect in onDraw()?
  • How android.view.View.invalidate(i nt l, int t, int r, int b) make only the dirty area redrawn?
like image 23
Macarse Avatar answered Sep 30 '22 05:09

Macarse