Invalidate

正常刷新

/**
* 使整个视图无效。如果视图可见,
* {@link #onDraw(android.graphics.Canvas)} 调用此方法后将在后续的UI刷新里调用onDraw(android.graphics.Canvas)方法
* <p>
* 必须从UI线程调用此方法。要从非UI线程调用,请调用{@link #postInvalidate()}.*/
public void invalidate() {
invalidate(true);
} /**
* This is where the invalidate() work actually happens. A full invalidate()
* causes the drawing cache to be invalidated, but this function can be
* called with invalidateCache set to false to skip that invalidation step
* for cases that do not need it (for example, a component that remains at
* the same dimensions with the same content).
*
* @param invalidateCache Whether the drawing cache for this view should be
* invalidated as well. This is usually true for a full
* invalidate, but may be set to false if the View's contents or
* dimensions have not changed.
* @hide
*/
public void invalidate(boolean invalidateCache) {
invalidateInternal(0, 0, mRight - mLeft, mBottom - mTop, invalidateCache, true);
}

一些解释

1.首先invalidate() 也是调用 invalidate(boolean invalidateCache) 这个方法的,只有设置为true时才会让这个View刷新

2.上面的注释已经说了invalidate()的刷新是必需在UI线程的

设置布局位置,重新调整View的刷新位置

/**
* Mark the area defined by dirty as needing to be drawn. If the view is
* visible, {@link #onDraw(android.graphics.Canvas)} will be called at some
* point in the future.
* <p>
* This must be called from a UI thread. To call from a non-UI thread, call
* {@link #postInvalidate()}.
* <p>
* <b>WARNING:</b> In API 19 and below, this method may be destructive to
* {@code dirty}.
*
* @param dirty the rectangle representing the bounds of the dirty region
*
* @deprecated The switch to hardware accelerated rendering in API 14 reduced
* the importance of the dirty rectangle. In API 21 the given rectangle is
* ignored entirely in favor of an internally-calculated area instead.
* Because of this, clients are encouraged to just call {@link #invalidate()}.
*/
@Deprecated
public void invalidate(Rect dirty) {
final int scrollX = mScrollX;
final int scrollY = mScrollY;
invalidateInternal(dirty.left - scrollX, dirty.top - scrollY,
dirty.right - scrollX, dirty.bottom - scrollY, true, false);
} /**
* Mark the area defined by the rect (l,t,r,b) as needing to be drawn. The
* coordinates of the dirty rect are relative to the view. If the view is
* visible, {@link #onDraw(android.graphics.Canvas)} will be called at some
* point in the future.
* <p>
* This must be called from a UI thread. To call from a non-UI thread, call
* {@link #postInvalidate()}.
*
* @param l the left position of the dirty region
* @param t the top position of the dirty region
* @param r the right position of the dirty region
* @param b the bottom position of the dirty region
*
* @deprecated The switch to hardware accelerated rendering in API 14 reduced
* the importance of the dirty rectangle. In API 21 the given rectangle is
* ignored entirely in favor of an internally-calculated area instead.
* Because of this, clients are encouraged to just call {@link #invalidate()}.
*/
@Deprecated
public void invalidate(int l, int t, int r, int b) {
final int scrollX = mScrollX;
final int scrollY = mScrollY;
invalidateInternal(l - scrollX, t - scrollY, r - scrollX, b - scrollY, true, false);
}

postInvalidate

这个方法可以非UI线程中调用

正常刷新

/**
* <p>导致在事件循环的后续循环中发生无效。使用此选项使非UI线程中的View无效</p>
*
* <p>仅当此视图附加到窗口时,才能从UI线程*外部调用此方法。</p>
*
* @see #invalidate()
* @see #postInvalidateDelayed(long)
*/
public void postInvalidate() {
postInvalidateDelayed(0);
}

改变位置后刷新

/**
* <p>Cause an invalidate of the specified area to happen on a subsequent cycle
* through the event loop. Use this to invalidate the View from a non-UI thread.</p>
*
* <p>This method can be invoked from outside of the UI thread
* only when this View is attached to a window.</p>
*
* @param left The left coordinate of the rectangle to invalidate.
* @param top The top coordinate of the rectangle to invalidate.
* @param right The right coordinate of the rectangle to invalidate.
* @param bottom The bottom coordinate of the rectangle to invalidate.
*
* @see #invalidate(int, int, int, int)
* @see #invalidate(Rect)
* @see #postInvalidateDelayed(long, int, int, int, int)
*/
public void postInvalidate(int left, int top, int right, int bottom) {
postInvalidateDelayed(0, left, top, right, bottom);
}

延迟刷新

/**
* <p>导致在事件*循环的后续循环中发生无效。等待指定的时间。</p>
*
* <p>This method can be invoked from outside of the UI thread
* only when this View is attached to a window.</p>
*
* @param delayMilliseconds the duration in milliseconds to delay the
* invalidation by
*
* @see #invalidate()
* @see #postInvalidate()
*/
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
final AttachInfo attachInfo = mAttachInfo;
if (attachInfo != null) {
attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
}
}

改变位置,并且延迟刷新

/**
* <p>Cause an invalidate of the specified area to happen on a subsequent cycle
* through the event loop. Waits for the specified amount of time.</p>
*
* <p>This method can be invoked from outside of the UI thread
* only when this View is attached to a window.</p>
*
* @param delayMilliseconds the duration in milliseconds to delay the
* invalidation by
* @param left The left coordinate of the rectangle to invalidate.
* @param top The top coordinate of the rectangle to invalidate.
* @param right The right coordinate of the rectangle to invalidate.
* @param bottom The bottom coordinate of the rectangle to invalidate.
*
* @see #invalidate(int, int, int, int)
* @see #invalidate(Rect)
* @see #postInvalidate(int, int, int, int)
*/
public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
int right, int bottom) { // We try only with the AttachInfo because there's no point in invalidating
// if we are not attached to our window
final AttachInfo attachInfo = mAttachInfo;
if (attachInfo != null) {
final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.obtain();
info.target = this;
info.left = left;
info.top = top;
info.right = right;
info.bottom = bottom; attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
}
}

end

Android开发 View的UI刷新Invalidate和postInvalidate的更多相关文章

  1. 50个Android开发人员必备UI效果源码[转载]

    50个Android开发人员必备UI效果源码[转载] http://blog.csdn.net/qq1059458376/article/details/8145497 Android 仿微信之主页面 ...

  2. android 开发 View _1_ View的子类们 和 视图坐标系图

    目录: android 开发 View _2_ View的属性动画ObjectAnimator ,动画效果一览 android 开发 View _3_ View的属性动画ValueAnimator a ...

  3. Android之界面刷新(invalidate和postInvalidate使用)

    Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用. Android提供了Inva ...

  4. Android中刷新Invalidate和postInvalidate的区别

    Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用.Android提供了Inval ...

  5. Android中View绘制流程以及invalidate()等相关方法分析(转)

    转自:http://blog.csdn.net/qinjuning 前言: 本文是我读<Android内核剖析>第13章----View工作原理总结而成的,在此膜拜下作者 .同时真挚地向渴 ...

  6. Android中View绘制流程以及invalidate()等相关方法分析

    [原文]http://blog.csdn.net/qinjuning 整个View树的绘图流程是在ViewRoot.java类的performTraversals()函数展开的,该函数做的执行过程可简 ...

  7. Android中View绘制流程以及invalidate()等相关方法分析(转载的文章,出处在正文已表明)

    转载请注明出处:http://blog.csdn.net/qinjuning 前言: 本文是我读<Android内核剖析>第13章----View工作原理总结而成的,在此膜拜下作者 .同时 ...

  8. Android开发——View滑动的三种实现方式

    0. 前言   Android开发中,我们常常需要View滑动实现一些绚丽的效果来优化用户体验.一般View的滑动可以用三种方式实现. 转载请注明出处:http://blog.csdn.net/seu ...

  9. Android开发——View滑动冲突解决方案

    0. 前言   我们在Android开发--事件分发机制详解中深入学习了事件分发机制,为我们解决Android开发中的滑动冲突问题做了初步准备.针对滑动冲突这里给出两种解决方案:外部拦截法和内部拦截法 ...

随机推荐

  1. mtk_Call setting(SS)

    1.SSDS: 2.PDN type 3.server回error之后的处理通过UT接口设置SS之后,网络有时候会回error,有些运营商会根据这些error来决定是否要CSFB, CSFB的条件如下 ...

  2. 访问nginx时验证密码

    只允许1.2.3.4这个IP访问我们的站点,拒绝所有其他IP的访问. server { listen 80; server_name www.example.com; satisfy any; all ...

  3. Linux文件介绍

    Linux文件介绍 Linux 文件属性 可以通过命令ll+文件名,查看文件的具体属性 例如:ll syz.gz 1736706 -rw-r--r--. 1 root root 28 Oct 27 1 ...

  4. 在SpringBoot 1.5.3上使用gradle引入hikariCP

    在SpringBoot 1.5.3上使用gradle引入hikariCP hikari来源于日语,是“光”的意思,号称“史上最快数据库连接池”,也是springboot2.0最新版默认的连接池.但是s ...

  5. C#中Json和类的相互转化

    //在NuGet里下载安装 Newtonsoft.Json,再引用. using Newtonsoft.Json; //写一个用户类 public class UserDataInfo { publi ...

  6. leetcode-第11场双周赛-5089-安排会议日程

    题目描述: 自己的提交: class Solution: def minAvailableDuration(self, slots1: List[List[int]], slots2: List[Li ...

  7. chkconfig 与 systemctl

    chkconfig命令 主要用来更新(启动或停止)和查询系统服务(service)的运行级信息,用于维护/etc/rc[0-6].d目录的命令行工具. chkconfig -–add httpd 增加 ...

  8. VS 2019企业版激活码

    Visual Studio 2019 EnterpriseBF8Y8-GN2QH-T84XB-QVY3B-RC4DF

  9. C++11的闭包(lambda、function、bind)

    c++11开始支持闭包,闭包:与函数A调用函数B相比较,闭包中函数A调用函数B,可以不通过函数A给函数B传递函数参数,而使函数B可以访问函数A的上下文环境才可见(函数A可直接访问到)的变量:比如: 函 ...

  10. js函数中的apply()、call()、bind()方法

    ECMAScript中的函数是对象,因此函数也有属性和方法.每个函数都包含两个属性:length和prototype,且每个函数包含两个非继承而来的方法apply()和call().这两个方法的用途都 ...