今天测试程序遇到一个问题,在缩放主线程的图片时,view得不到及时刷新,这是就要靠手动刷新,我用了postInvalidate这个方法。
postInvalidate和Invalidate都能进行view的刷新,但两个方法是有区别的,前者是在非UI线程中使用,而后者在UI线程自身中使用。
View 类中postInvalidate()方法源码如下,可见它也是用到了handler的:
- public void postInvalidate() {
- postInvalidateDelayed(0);
- }
- public void postInvalidateDelayed(long delayMilliseconds) {
- // We try only with the AttachInfo because there's no point in invalidating
- // if we are not attached to our window
- if (mAttachInfo != null) {
- Message msg = Message.obtain();
- msg.what = AttachInfo.INVALIDATE_MSG;
- msg.obj = this;
- mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
- }
- }
本文介绍了在Android开发中,解决主线程图片缩放导致View刷新不及时的问题。通过对比postInvalidate和Invalidate两种方法,详细解释了它们的区别及应用场景。
236

被折叠的 条评论
为什么被折叠?



