@Override
protected void onRestart() {
super.onRestart(); /onRestart中开启新线程,更新UI/
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ": " + Thread.currentThread().getId());
tv.setText(“update UI is success!”);
btn.setText(“update UI is success!”);
}
});
thread.start();
}
不好意思,按下返回按钮在启动程序,或者按 Home 键再启动程序,就这么折腾几下,就会包异常!信息如下:
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
意思是:只有主线程才可以更新 UI。
解决办法:加上 postInvalidate()
方法。
@Override protected void onRestart() {
super.onRestart(); /onRestart中开启新线程,更新UI/
Thread thread = new Thread(new Runnable() {
@Override public void run() {
System.out.println(Thread.currentThread().getName() + ": " + Thread.currentThread().getId());
tv.postInvalidate(); btn.postInvalidate();
tv.setText(“update UI is success!”);
btn.setText(“update UI is success!”);
}
});
thread.start();
}
postInvalidate()
方法,源码:
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);
}
}
其实,是调用了 Handler 的处理消息的机制!该方法可以在子线程中直接用来更新UI。还有一个方法 invalidate (),稍候再说!
3. 在 Button 的事件中开启线程,更新 UI
public class MasterActivity extends Activity {
TextView tv = null; Button btn = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println(Thread.currentThread().getName() + ": " + Thread.currentThread().getId());
tv = (TextView)findViewById(R.id.text);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Thread thread = new Thread(new Runnable() {
@Override public void run() {
System.out.println(Thread.currentThread().getName() + ": " + Thread.currentThread().getId());
tv.setText(“update UI is success!”);
btn.setText(“update UI is success!”);
}
});
thread.start();
}
});
}
Sorry,报错!即使你加上 postInvalidate()
方法,也会报这个错误。
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
4. 使用 Handler 结合多线程更新 UI
a. 开启一个线程,在 run 方法中通知 Handler
b. Handler 中使用 handleMessage 方法更新 UI
5. Handler 和 invalidate 方法结合多线程更新 UI
方法 invalidate
主要用在主线程中(即UI 线程中),不可以用于子线程。如果在子线程中需要使用 postInvalidate 方法。
sdk 的 api 有说明:
public void invalidate () Since: API Level 1 Invalidate the whole view.
If the view is visible, onDraw(Canvas) will be called at some point in the future.
This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().
看看该方法源码:
public void invalidate() {
if (ViewDebug.TRACE_HIERARCHY) {
ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
}
if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS)) {
mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;
final ViewParent p = mParent;
final AttachInfo ai = mAttachInfo;
if (p != null && ai != null) {
final Rect r = ai.mTmpInvalRect;
r.set(0, 0, mRight - mLeft, mBottom - mTop); // Don’t call invalidate – we don’t want to internally scroll // our own bounds p.invalidateChild(this, r); } } }
}
}
}
invalidate
方法如果你直接在主线程中调用,是看不到任何更新的。需要与Handler结合!
感谢这位“雷锋”,一个不错的例子:http://disanji.net/2010/12/12/android-invalidate-ondraw/
只是被我修改了一点,加入times,看看 onDraw 到底运行多少次。
Android 在 onDraw 事件处理绘图,而 invalidate() 函数可以再一次触发 onDraw 事件,然后再一次进行绘图动作。
public class MasterActivity extends Activity {
static int times = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( new View(null){
Paint vPaint = new Paint(); //绘制样式物件
private int i = 0; //弧形角度
@Override
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);
System.out.println(“this run " + (times++) +” times!");
// 设定绘图样式
vPaint.setColor( 0xff00ffff ); //画笔颜色
vPaint.setAntiAlias( true ); //反锯齿
vPaint.setStyle( Paint.Style.STROKE );
// 绘制一个弧形
canvas.drawArc(new RectF(60, 120, 260, 320), 0, i, true, vPaint );
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
深开发者,这些资料都将为你打开新的学习之门**
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!