7

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?

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
imax366
  • 71
  • 1
  • 3

3 Answers3

3

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.

Kevin Read
  • 2,173
  • 16
  • 23
  • I am a novice to android and I have a question here http://stackoverflow.com/questions/21027199/am-i-invalidating-the-entire-screen-on-every-call Please provide the answer if possible. – Kraken Jan 09 '14 at 18:18
1

I guess that code is in the View class.

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

Macarse
  • 91,829
  • 44
  • 175
  • 230
  • -1 as both links are now dead. Including some of their content here would have been better. – Mark Whitaker Jan 25 '13 at 12:08
  • 1
    +1 because the links are not dead... I just tested them. However, I agree that adding content is a good thing to do in cases like this. – Justin Jul 15 '13 at 19:53
0

I think android draw the view just like canvas in j2me, all the draw code is not draw directly to hardware ( screen ), it just draw to a buffer or drawable or something. When we call View.invalidate(Rect), teh system copy the image in the rect of the buffer( or drawable) to hardware(screen).

xtr
  • 5,870
  • 1
  • 21
  • 23