什么时候需要在视图上执行 invalidate()? [英] When it's necessary to execute invalidate() on a View?

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

问题描述

我对这个问题的回答刚刚被接受,但我开始怀疑什么时候需要使()一个视图失效,什么时候不需要?

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() 之后更改某些内容(例如作为对按钮单击的响应,我应该 invalidate() 更改后的 查看).

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所说,它必须是比那更复杂,这取决于人们使用的方法.

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();

那么,什么时候需要在视图上执行 invalidate() 以及它是如何工作的?

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

推荐答案

通常,系统会自动为您的小部件处理调整大小、隐藏、显示和大量其他事情,但如果绘制像素的底层缓冲区或支持数据已更改或陈旧(您交换视图上的图像资源或原始数据集更改).发生这种情况是因为操作系统无法知道数据以特定方式更改.

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 版本,系统跟踪更改的内容会有所不同,但我通常做的是假设系统资源(字节数组、字符数组、资源索引、上下文手动绘制)没有被跟踪并且需要一个 invalidate,其他一切都由系统处理.

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.

这篇关于什么时候需要在视图上执行 invalidate()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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