Setimageresource and Setimagedrawable differences

Source: Internet
Author: User

ImageView the way to set up the picture has many clocks, can write in the XML android:src= "@drawable/xxx", can also be set in Java code.

There are several ways to set up in Java, including:Setimageresource,setimagedrawable,setimagebitmap.

In the XML is set in fact and in the Java call Setimageresource is the same, of course, XML more than a parsing process, put in Java code call will be slightly better (actually no difference).

3 different ways to set up pictures:

(1) The setimageresource parameter is resid and must be a resource in the drawable directory. In addition, comments are indicated in the Setimageresource method

* <pclass= "note" >this does Bitmap reading and decoding on the UI

* thread, which can cause a latency hiccup. If that ' s a concern,

* Consider using {@link #setImageDrawable (android.graphics.drawable.Drawable)}or

* {@link #setImageBitmap (Android.graphics.Bitmap)} and

* {@link android.graphics.BitmapFactory} instead.</p>

This method reads and parses the picture in the UI thread, so it is possible to delay the start of an activity. So if you are concerned about this official proposal to replace it with Setimagedrawable and Setimagebitmap.

(2) The setimagebitmap parameter is bitmap, which can parse images from different sources and set them up. But let's look at the source code of Setimagebitmap:

@android. View.remotableviewmethod

public void Setimagebitmap (Bitmap bm) {

If this is used frequently, mayhandle bitmaps explicitly

To reduce the intermediate drawable object

Setimagedrawable (Newbitmapdrawable (Mcontext.getresources (), BM));

}

What Setimagebitmap actually do is to encapsulate the bitmap object as a Drawable object, and then call Setimagedrawable to set the picture. Therefore, the code is written in the suggestion, if you need to call this method frequently, it is best to encapsulate a fixed drawable object, call setimagedrawable directly, this can reduce the Drawable object. Because each call to the Setimagebitmap method will have a drawable to the bitmap object.

(3) setimagedrawable parameter is drawable, but also can accept different sources of pictures, the way to do is to update the ImageView picture. The last two methods actually call the Setimagedrawable (Setimageresource is not called directly, but the updated method is the same as setimagedrawable).

So a comprehensive view of setimagedrawable is the most efficient memory, if you worry about the picture too large or too many images affect memory and load efficiency, you can parse the picture and then call the Setimagedrawable method to set.

Setimageresource and Setimagedrawable differences

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.