View的invalidate和postInvalidate
程序员文章站
2022-07-11 08:27:13
...
自定义View,如果要刷新,重绘View,可以利用invalidate和postInvalidate两个方法,当然还有其他方法可以实现重绘(requestLayout会导致重新进行测量,布局,绘制),本文重点讲解这两个函数的异同。
invalidate和postInvalidate常用于刷新View,都会导致View重绘,重新调用view的onDraw方法。
invalidate必须在ui线程使用,postInvalidate可以在子线程中使用。
/**
* Invalidate the whole view. If the view is visible,
* {@link #onDraw(android.graphics.Canvas)} will be called at some point in
* the future.
* <p>
* 必须在UI线程执行,postInvalidate可以不再UI线程执行。
* This must be called from a UI thread. To call from a non-UI thread, call
* {@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);
}
/**
* <p>Cause an invalidate 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>
*
* @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);
}
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);
}
}
//postInvalidate方法内部使用了Handler
public void dispatchInvalidateDelayed(View view, long delayMilliseconds) {
Message msg = mHandler.obtainMessage(MSG_INVALIDATE, view);
mHandler.sendMessageDelayed(msg, delayMilliseconds);
}
postInvalidate内部利用Handler实现,所以可以利用Handler和invalide实现postInvalidate功能。
推荐阅读
-
android中Invalidate和postInvalidate的更新view区别
-
Android中父View和子view的点击事件处理问题探讨
-
android自定义ListView实现底部View自动隐藏和消失的功能
-
go切片的copy和view的使用方法
-
android自定义ListView实现底部View自动隐藏和消失的功能
-
微信小程序实现动态改变view标签宽度和高度的方法【附demo源码下载】
-
使用WP post view插件和roundaboutJS制作的博客热门文章展区
-
android view getWidth 和 getHeight 的值为0
-
Android之View的知识(getWidth() 和getMeasuredWidth区别 如何在oncreate获取宽高)
-
自定义View-柱状图和折线图的合体