轉載請註明原文出處
===============================================================================
總是感覺android中UI更新很讓人糾結!自己小結一下,算是拋磚引玉。讀這篇文章之前,假設你已經明白線程、handler的使用。
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根本是風牛馬不相及!
不知道爲什麼在這裏開啓子線程更新UI就沒有問題!真的想不明白????
2. 在activity如onResume、onStart、反正是以on開頭的回調方法
- @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.
- public class MasterActivity extends Activity {
- TextView tv = null;
- Button btn = null;
- Handler mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- if(msg.what == 1) {
- tv.setText("update UI is success!");
- btn.setText("update UI is success!");
- }
- super.handleMessage(msg);
- }
- };
- @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());
- Message msg = mHandler.obtainMessage();
- msg.what = 1;
- msg.sendToTarget();
- }});
- thread.start();
- }
- });
- }
- 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);
- }
- }
- }
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的使用,還待探索。革命尚未成功,同志仍需努力!