开发者

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

开发者 https://www.devze.com 2022-12-25 23:47 出处:网络
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

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 ImageV开发者_JAVA百科iew.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?


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.


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?


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消