欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

真的必须要在主线程更新UI吗?

程序员文章站 2022-03-21 22:21:25
一般UI更新都会调用requestLayout 或者invalidate, 自定义view 也要如此 ???。@android.view.RemotableViewMethodpublic void setWidth(int pixels) {mMaxWidth = mMinWidth = pixels;mMaxWidthMode = mMinWidthMode = PIXELS;requestLayout();invalidate();}@Overridepublic void requ...

一般UI更新都会调用requestLayout 或者invalidate, 自定义view 也要如此 ???。
@android.view.RemotableViewMethod
public void setWidth(int pixels) {
mMaxWidth = mMinWidth = pixels;
mMaxWidthMode = mMinWidthMode = PIXELS;
requestLayout();
invalidate();
}
@Override
public void requestLayout() {//不会调用draw
if (!mHandlingLayoutInLayoutRequest) {
checkThread();
mLayoutRequested = true;
scheduleTraversals();// 1
}
}
void checkThread() {
if (mThread != Thread.currentThread()) {
throw new CalledFromWrongThreadException(
“Only the original thread that created a view hierarchy can touch its views.”);
}
}

而在onCreate直接去更新UI,不走上述requestLayout 或者invalidate。其堆栈是这样的:

2020-08-09 09:39:48.374 3561-3561/com.yz.myapplication D/yyzz: android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614) android.app.ActivityThread
2020-08-09 09:39:48.374 3561-3561/com.yz.myapplication D/yyzz: com.yz.myapplication.MainActivit

本文地址:https://blog.csdn.net/qq_42894864/article/details/107890763