Android-several methods for updating the UI by threads

Source: Internet
Author: User

Android-several methods for updating the UI by threads

To update the UI, a new thread on Android needs to jump to the main thread to perform operations. The following are some solutions provided by foreigners. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + CjxwPjHNqLn9VUm/2Lz + placement = "brush: java;"> private void loadIcon () {new Thread (new Runnable () {@ Overridepublic void run () {try {Thread. sleep (mDelay);} catch (InterruptedException e) {e. printStackTrace ();} mBitmap = BitmapFactory. decodeResource (getResources (), R. drawable. painter); mImageView. post (new Runnable () {@ Overridepublic void run () {mImageView. setImageBitmap (mBitmap );}});}}). start ();}
2. Call the main thread runOnUiThread to execute the modification.

new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(mDelay);} catch (InterruptedException e) {e.printStackTrace();}mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.painter);SimpleThreadingExample.this.runOnUiThread(new Runnable() {@Overridepublic void run() {mImageView.setImageBitmap(mBitmap);}});}}).start();

3. Use Handler to continuously post a runnable on another thread to execute the modification:

private class LoadIconTask implements Runnable {int resId;LoadIconTask(int resId) {this.resId = resId;}public void run() {handler.post(new Runnable() {@Overridepublic void run() {mProgressBar.setVisibility(ProgressBar.VISIBLE);}});mBitmap = BitmapFactory.decodeResource(getResources(), resId);// Simulating long-running operationfor (int i = 1; i < 11; i++) {sleep();final int step = i;handler.post(new Runnable() {@Overridepublic void run() {mProgressBar.setProgress(step * 10);}});}handler.post(new Runnable() {@Overridepublic void run() {mImageView.setImageBitmap(mBitmap);}});handler.post(new Runnable() {@Overridepublic void run() {mProgressBar.setVisibility(ProgressBar.INVISIBLE);}});}}

4. the following example uses Handlemessage to perform operations. It is interesting to use soft references. (Refer to the weak keyword in IOS). When handler sends a message, the obtainMessage adopts the pool recycle mechanism.

static class UIHandler extends Handler {WeakReference
 
   mParent;public UIHandler(WeakReference
  
    parent) {mParent = parent;}@Overridepublic 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
   
    (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();}}}
   
  
 

5. AyncTask is also a common method.

class LoadIconTask extends AsyncTask
 
   {@Overrideprotected void onPreExecute() {mProgressBar.setVisibility(ProgressBar.VISIBLE);}@Overrideprotected 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;}@Overrideprotected void onProgressUpdate(Integer... values) {mProgressBar.setProgress(values[0]);}@Overrideprotected 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());}}}
 

In any case, I just want to express that foreigners are sometimes quite interesting.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.