android-threads update the UI in several ways

Source: Internet
Author: User

, Android on the new line Cheng want to update the UI, need to jump to the main thread in order to operate, the following is the foreigner to give a few programs, we learn a lot.


1 post a runnable subclass through the UI control:

private void LoadIcon () {New Thread (new Runnable () {@Overridepublic void run () {try {thread.sleep (mdelay);} catch (Interru Ptedexception e) {e.printstacktrace ();} Mbitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.painter); Mimageview.post (new Runnable () {@ overridepublic void Run () {mimageview.setimagebitmap (MBITMAP);}}). Start ();}

2. Perform the modification by calling the main thread Runonuithread

New Thread (New Runnable () {@Overridepublic void run () {try {thread.sleep (mdelay);} catch (Interruptedexception e) {E.prin Tstacktrace ();} Mbitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.painter); SimpleThreadingExample.this.runOnUiThread (New Runnable () {@Overridepublic void run () {Mimageview.setimagebitmap ( MBITMAP);}). Start ();

3. Perform the modification by handler the post one runnable on the other thread continuously:

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 < 1 1;  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 is done by Handlemessage, and it is interesting to use a soft reference. (Refer to the weak keyword in ios), while handler sends a message obtainmessage uses a pool recycling mechanism.

Static class Uihandler extends Handler {weakreference
5. Adopt the Aynctask way, is also our more commonly used way

Class Loadicontask extends Asynctask<integer, Integer, bitmap> {@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 <; i++) {sleep ();p ublishprogress (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 ());}}}

No matter what. Hair this thing, just want to express, the foreigner's things are sometimes quite interesting.


android-threads update the UI in several ways

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.