Handler: 更新UI的方法
总是感觉 android 中 UI 更新很让人纠结!自己小结一下,算是抛砖引玉。
读这篇文章之前,假设你已经明白线程、Handler 的使用。
在文章的最后,附录一张草图,主要用于说明 Handler、Message、MessageQueue、Looper 之间的关系。
1. 在 onCreate() 方法中开启线程更新 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); /*onCreate中开启新线程,更新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(); }
随便折腾,不会报错或者异常!
以为开启的线程和 UI 线程(主线程)是同一个线程,但是很不幸,他们的线程id根本是风牛马不相及!
大家可以跟踪一下android 源码,这个主要是因为在加载 Activity 的时候,还没有触发检查单线程的模型(即子线程不可以更新ui)。
如果你不相信的话,可以在上面的线程里面 `while true`,那么一定会报错的。
2. 在 activity 如 onResume、onStart、反正是以 on 开头的回调方法
@Overrideprotected 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 ); // 弧形角度 if( (i+=10) > 360 ) { i = 0; } // 重绘, 再一次执行onDraw 程序 invalidate(); } }); }}
经过测试,发现 times 一直在++,说明 onDraw 被多次调用,并且一致在画图!
SDK 的 API 有时候让人很郁闷,无语.....关于 invalidate 的使用,还待探索。革命尚未成功,同志仍需努力!
博客更新,推荐文章:
View编程(2): invalidate()再探
View编程(3): invalidate()源码分析
附录: Handler、Message、MessageQueue、Looper 之间的关系
这里说明
1. Looper 使用无限循环取出消息,是有 android os 控制的。
2. android 线程是非安全的,即不要在子线程中更新 UI。
3. Looper 取出来的消息,handler 可以通过 what、obj 等量来区别分别获取属于自己的消息,所以推荐使用这些量。
欢迎大家关注我的个人微信公众号: ITMan,微信公众号搜索: ITManMark,或者扫描下图二维码.
无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点 这里 可以跳转到教程。
更多相关文章
- Android(安卓)AsyncTask 源码解析
- Android通过获取Ip的方法判断手机是否联网
- android 为什么初始界面scrollView.scrollTo执行无效
- android中google“定位查询”
- Android(安卓)非UI线程使用View.post()方法一处潜在的内存泄漏
- Android(安卓)事件总线EventBus——— 设计原理
- Android(安卓)注解
- 安卓四大组件之——ContentProvider学习
- Android底部fragment互相跳转