Am I invalidating the entire screen on every call?

末鹿安然 提交于 2019-12-21 17:23:01

问题


I am new to Android Development and reading the book Hello Android. It uses a Sudoku example, and the code that I am referring to is here.

In this , onTouchScreen, it calls select method, that calls invalidate twice. The question is that, on invalidating is the onDraw method called right after that? So will in this case, inside my select method, it will do

  1. invalidate
  2. call onDraw
  3. Do some stuff
  4. invalidate
  5. call onDraw

Is this how it will happen, also, will the entire screen be regenerated? All the numbers and hints etc., because from the book the author says

In an earlier version of this example, I invalidated the entire screen whenever the cursor was moved. Thus, on every key press, the whole puzzle had to be redrawn. This caused it to lag noticeably. Switching the code to invalidate only the smallest rectangles that changed made it run much faster.

What exactly is he trying to say here?

Added Info

I added some logs in the onDraw method, some at the starting, some in the for loop. Whenever I touched a new rectangle, all the logs were called. Doesnt that mean that the entire screen is geting repopulated, since all the code in onDraw is reexecuted?


回答1:


Kraken

Q: But what about the logs, surely if my loops are getting executed it means that all the canvas.draw will be getting executed too?
A: Yes, the whole drawing will be executed in your sample code. You have to optimize the rendering process by yourself, in onDraw method.

Q: How does the system know, what piece of code will "only" redraw the dirty area?
A: Canvas::getClipBounds will give you a dirty rect, which you should draw something on.
Inside your for loop in onDraw, compare the dirty rect with the rect which you want to draw. Then do continue if they do not intersect.

But remember, if you have several area set to dirty, the returned rect will be a union of all dirty areas.
Please see the following two questions below:
Getting the dirty region inside draw()
Android: invalidate(dirty)

Hope this will help you.

==========================

The author is right. But this still can be optimized.

Calling invalidate(Rect) will automatically set a clip area for the canvas. (That's why canvas.getClipBounds() can return that area).
Then, during onDraw(), anything drawing out of the clip area, will be ignored. They do not appear on the screen, so it REALLY reduce the time of drawing.
But ignoring them still costs overhead. Therefore, for graphical intensive app, onDraw() could be better optimized if you exclude them in advance.

You can find a great example for optimizing onDraw() in android's KeyboardView, which provide the view of your android's input method. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/inputmethodservice/KeyboardView.java




回答2:


This is directly from View documentation:

Drawing is handled by walking the tree and rendering each view that intersects the invalid region. Because the tree is traversed in-order, this means that parents will draw before (i.e., behind) their children, with siblings drawn in the order they appear in the tree. If you set a background drawable for a View, then the View will draw it for you before calling back to its onDraw() method.

Note that the framework will not draw views that are not in the invalid region.`

From what I understand, once your view is drawn for the first time, a tree if formed with parent and child objects along with their positions on screen. When you pass a designated area to invalidate, this tree is checked for effected nodes in that area and only those nodes will be called for draw.

Now what I also don't understand is that in this example, the only View is the PuzzleView. I'm not sure how a drawing a single view can be optimized. Check if it is discussed further in the text.

If it is not, then my theory would be that the canvas objects(rectangles) are also part of the above said tree and only those parts, i.e. the rectangles in the specified area are drawn.

More importantly, do you see any improvement after using area invalidate vs full invalidate?




回答3:


Even if you call invalidate multiple times the onDraw method will only be called once. Basically the onDraw gets called inside the RunLoop method when a view has been invalidated. That means that if you invalidate the view multiple times before giving back the control to the runloop the view will be redrawn only once. Notice that if you invalidate two different rects of the view the system will try to make an union of those rects before redrawing your view.




回答4:


In the code, the invalidate that you are talking about is this:

  invalidate(selRect);

?

If it is he only calls the onDraw of this selected rectangle selRect.

Only the invalidate(); redraws the hole screen.

Hope it helps.




回答5:


On this example, you should notice that invalidate() calls have a Rect as parameter. This mean that only this zone of the view is getting dirty and is going to be redrawn by the system.

Calling invalidate() will not trigger the onDraw() method right after. The system only decide whenever he wants to redraw the view.

From Android documentation :

If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future.

Knowing that, inside the select method, this will probably happen : 1. Invalidate a small portion of the View 2. Do some stuff 3. Invalidate another small portion of the View 4. Theses 2 portions of the View are getting redrawn

Hope that helped.



来源:https://stackoverflow.com/questions/21027199/am-i-invalidating-the-entire-screen-on-every-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!