标签:android style blog http os io java ar for
如图,Android上新开的线程如想更新UI,需要重新跳到主线程中才能操作,以下是老外给出的几种方案,大家多多学习下.
1通过UI控件post一个runnable子类:
private void loadIcon() { new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(mDelay); } catch (InterruptedException e) { e.printStackTrace(); } mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.painter); mImageView.post(new Runnable() { @Override public void run() { mImageView.setImageBitmap(mBitmap); } }); } }).start(); }
new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(mDelay); } catch (InterruptedException e) { e.printStackTrace(); } mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.painter); SimpleThreadingExample.this.runOnUiThread(new Runnable() { @Override public void run() { mImageView.setImageBitmap(mBitmap); } }); } }).start();
private class LoadIconTask implements Runnable { int resId; LoadIconTask(int resId) { this.resId = resId; } public void run() { handler.post(new Runnable() { @Override public void run() { mProgressBar.setVisibility(ProgressBar.VISIBLE); } }); mBitmap = BitmapFactory.decodeResource(getResources(), resId); // Simulating long-running operation for (int i = 1; i < 11; i++) { sleep(); final int step = i; handler.post(new Runnable() { @Override public void run() { mProgressBar.setProgress(step * 10); } }); } handler.post(new Runnable() { @Override public void run() { mImageView.setImageBitmap(mBitmap); } }); handler.post(new Runnable() { @Override public void run() { mProgressBar.setVisibility(ProgressBar.INVISIBLE); } }); } }
static class UIHandler extends Handler { WeakReference<HandlerMessagesActivity> mParent; public UIHandler(WeakReference<HandlerMessagesActivity> parent) { mParent = parent; } @Override public void handleMessage(Message msg) { HandlerMessagesActivity parent = mParent.get(); if (null != parent) { switch (msg.what) { case SET_PROGRESS_BAR_VISIBILITY: { parent.getProgressBar().setVisibility((Integer) msg.obj); break; } case PROGRESS_UPDATE: { parent.getProgressBar().setProgress((Integer) msg.obj); break; } case SET_BITMAP: { parent.getImageView().setImageBitmap((Bitmap) msg.obj); break; } } } } } Handler handler = new UIHandler(new WeakReference<HandlerMessagesActivity>( this)); private class LoadIconTask implements Runnable { private final int resId; private final Handler handler; LoadIconTask(int resId, Handler handler) { this.resId = resId; this.handler = handler; } public void run() { Message msg = handler.obtainMessage(SET_PROGRESS_BAR_VISIBILITY, ProgressBar.VISIBLE); handler.sendMessage(msg); final Bitmap tmp = BitmapFactory.decodeResource(getResources(), resId); for (int i = 1; i < 11; i++) { sleep(); msg = handler.obtainMessage(PROGRESS_UPDATE, i * 10); handler.sendMessage(msg); } msg = handler.obtainMessage(SET_BITMAP, tmp); handler.sendMessage(msg); msg = handler.obtainMessage(SET_PROGRESS_BAR_VISIBILITY, ProgressBar.INVISIBLE); handler.sendMessage(msg); } private void sleep() { try { Thread.sleep(mDelay); } catch (InterruptedException e) { e.printStackTrace(); } } }
class LoadIconTask extends AsyncTask<Integer, Integer, Bitmap> { @Override protected void onPreExecute() { mProgressBar.setVisibility(ProgressBar.VISIBLE); } @Override protected Bitmap doInBackground(Integer... resId) { Bitmap tmp = BitmapFactory.decodeResource(getResources(), resId[0]); // simulating long-running operation for (int i = 1; i < 11; i++) { sleep(); publishProgress(i * 10); } return tmp; } @Override protected void onProgressUpdate(Integer... values) { mProgressBar.setProgress(values[0]); } @Override protected void onPostExecute(Bitmap result) { mProgressBar.setVisibility(ProgressBar.INVISIBLE); mImageView.setImageBitmap(result); } private void sleep() { try { Thread.sleep(mDelay); } catch (InterruptedException e) { Log.e(TAG, e.toString()); } } }
标签:android style blog http os io java ar for
原文地址:http://blog.csdn.net/qq285016127/article/details/39030567