The difference between Android ImageView Setimagedrawable and Setimageresource

Source: Internet
Author: User

1. The Setimageresource is synchronized, and the resource images are read and decoded in the main thread. The setimagedrawable is asynchronous.

2. Difference in loading speed. Setimageresource faster than Setimagedrawable and Setimagebitmap.

/*** Sets a drawable as the content of this ImageView. * * <p class= "Note" >this does Bitmap reading and decoding on the UI * thread, which can cause a latency HICC  Up. If that ' s a concern, * consider using {@link#setImageDrawable (android.graphics.drawable.Drawable)} or * {@link#setImageBitmap (ANDROID.GRAPHICS.BITMAP)} and * {@linkAndroid.graphics.BitmapFactory} instead.</p> * *@paramresId The resource identifier of the drawable * * @attr ref Android. R.STYLEABLE#IMAGEVIEW_SRC*/@android. View.remotableviewmethod Public voidSetimageresource (intresId) {        if(MUri! =NULL|| Mresource! =resId) {            Final intOldWidth =Mdrawablewidth; Final intOldHeight =Mdrawableheight; Updatedrawable (NULL); Mresource=resId; MUri=NULL;            ResolveUri (); if(oldwidth! = Mdrawablewidth | | OldHeight! =Mdrawableheight) {requestlayout ();        } invalidate (); }    }

 /*** Sets a drawable as the content of this ImageView. *      * @paramdrawable the drawable to set*/     Public voidsetimagedrawable (drawable drawable) {if(Mdrawable! =drawable) {Mresource= 0; MUri=NULL; Final intOldWidth =Mdrawablewidth; Final intOldHeight =Mdrawableheight;            Updatedrawable (drawable); if(oldwidth! = Mdrawablewidth | | OldHeight! =mdrawableheight)            {requestlayout ();        } invalidate (); }    }

/**      * Sets a Bitmap as the content of this ImageView.     *      @param  BM The bitmap to set     */    @android. View.remotableviewmethod      Public void Setimagebitmap (Bitmap BM) {        //  If this was used frequently, may handle bitmaps explicitly< c18/>/ to reduce the intermediate drawable object        setimagedrawable (new bitmapdrawable (Mcontext.getresources (), BM);    }

The difference between Android ImageView Setimagedrawable and Setimageresource

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.