当有必要在一个视图下执行无效()? [英] When it's necessary to execute invalidate() on a View?

查看:171
本文介绍了当有必要在一个视图下执行无效()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的回答<一href="http://stackoverflow.com/questions/10607821/swapping-image-in-imageview/10608396#10608396">this问题只是接受,但我一开始的时候只有一个需要无效怀疑()视图,并在没有必要?

My answer to this question was just accepted but I started to wonder when exactly one needs to invalidate() a View and when it is not necessary?

在一个有点想法我来实现它的应该的工作或多或少是这样的:

After a bit of thinking I came to realization that it should work more or less like this:

  • 的一切实际绘图发生后 onResume()
  • 在屏幕的空闲时间部分可以被重绘,但只有那些被无效(一切下)
  • actual drawing of "everything" occurs after onResume()
  • in "free" time parts of the screen can be redrawn but only those that were invalidated (and everything underneath)

因此​​,它似乎,如果我改变后的东西 onResume()(例如,作为一个按钮点击的响应,我应该无效( )的修改查看)。

Therefore, it would seem, if I change something after onResume() (e.g. as a response to a button click, I should invalidate() the changed View).

不过,从什么SCANA的<一个href="http://stackoverflow.com/questions/10607821/swapping-image-in-imageview/10608396#10608396">this问题说,这一定是更加复杂那么,它依赖somethimes的人使用什么方法。

However, from what scana in this question says, it must be more complex then that and it depends somethimes on what method one uses.

例如。一个人是否使用

lastClicked.setImageBitmap();

lastClicked.setImageResource();

所以,当有必要在一个视图下执行无效(),它是如何真正发挥作用?

So, when it's necessary to execute invalidate() on a View and how does it really work ?

推荐答案

通常情况下,系统处理调整大小,隐藏,自动显示和一吨的其他东西为你的小部件,但有时也有问题,如果绘制的像素的基础缓冲区或依托数据已更改或已过时(你掉在一个视图或原始数据集发生变化的图像资源)。此发生的原因是没有办法,OS可以知道数据中,它没有特定的方式改变。

Usually, the system handles resizing, hiding, showing and a ton of other things for your widgets automatically but it sometimes has issues if the underlying buffer for drawn pixels or backing data has changed or is stale (you swap the image resource on a View or the raw dataset changes). This occurs because there is no way that the OS can know that the data changed in the specific manner that it did.

在这种情况下,你正在处理的图纸,你要告诉它的底层数据是不是在一个良好的状态的 Widget.invalidate()的和重画被排队上系统正如你所提到的主线程。根据系统实施和Android版本是什么系统跟踪的变化而变化,但我通常做的是假设系统资源(字节数组,字符数组,资源指标,上下文手工绘图)是不被跟踪,需要一个无效的和一切都会被系统处理。

In these cases where you are dealing with drawing, you have to tell the system that its underlying data is not in a good state with Widget.invalidate() and the re-drawing gets queued on the main thread just as you mentioned. Depending on the system implementation and Android version what is tracked for changes by the system varies but what I normally do is assume that system resources (byte arrays, char arrays, resource indexes, manual drawing on the context) are not tracked and need an invalidate and everything else will be handled by the system.

这篇关于当有必要在一个视图下执行无效()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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