我是不是无效在每次调用整个屏幕? [英] Am I invalidating the entire screen on every call?

查看:123
本文介绍了我是不是无效在每次调用整个屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid开发和读的书您好安卓。它采用了数独例子,code,我指的是<一个href="http://media.pragprog.com/titles/eband3/$c$c/Sudokuv2/src/org/example/sudoku/PuzzleView.java">here.

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.

在此,onTouchScreen,它调用选择方法,即调用无效的两倍。现在的问题是,在无效被称为之后的的OnDraw 的方法?所以会在这种情况下,我的选择方法中,它会做

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. 无效
  2. 在调用的OnDraw
  3. 在做一些东西
  4. 无效
  5. 在调用的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

在这个例子中的早期版本,我无效整个   每当屏幕光标被感动了。因此,在每一个关键   preSS,整个拼图不得不重新绘制。这导致它落后   明显。切换code只最小无效   这改变了矩形使它运行更快。

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?

新增信息

我添加了一些日志中的OnDraw方法,有的在起跑线,有的在for循环。每当我感动了一个新的矩形,所有的日志被称为。犯规是不是意味着整个画面歌厅重新填充,因为所有的code中的OnDraw被重新执行?

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?

推荐答案

海妖

问:但是,我们的日志,当然,如果我的回路得到执行这意味着所有的canvas.draw将会得到执行过
? 答:是的,整个图形将您的样品code执行。你必须自己优化渲染过程中,的OnDraw 方法。

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.

问:系统如何知道哪段code将只重绘脏区
答:画布:: getClipBounds 会给你一个脏矩形,你应该画点什么的
在你的的OnDraw 循环,比较脏矩形与要绘制的矩形。然后做继续,如果他们不相交。

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)

希望这会帮助你。

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

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

作者是正确的。但是这仍然可以进行优化。

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

调用无效(矩形)将自动设置剪辑区域的画布。 (这就是为什么 canvas.getClipBounds()可以返回该区域)。
然后,在的OnDraw(),什么都抽出夹区域,将被忽略。它们不会出现在屏幕上,因此它的真正的降低绘图的时间。
但他们忽略了还是成本开销。因此,对于图形密集型应用程序,的OnDraw()如果排除他们提前可以更好地优化。

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.

您可以找到优化一个很好的例子的OnDraw()在Android的 KeyboardView ,其提供的视图中的android的输入法。 <一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/1.5_r4/android/inputmethodservice/KeyboardView.java" rel="nofollow">http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/1.5_r4/android/inputmethodservice/KeyboardView.java

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

这篇关于我是不是无效在每次调用整个屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆