Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML vs. setImageDrawable/setImageBitmap

It is advantageous in my application if I preload certain images. I do this correctly, in AsyncTask, as it is written in the official documentation. But I have a problem/question about when they should be set.

I'll show code snippets. Note that it's simplified (their interoperability is better in my real code, it checks for nulls, etc.).

Let's see the original (non-preloaded) version first:

<ImageView
    android:id="@+id/imageViewMyGraphicalImageElement"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="centerCrop" 
    android:src="@drawable/my_graphical_element" >
</ImageView>

The preloaded version has the following XML (notice that the src attribute is missing):

<ImageView
    android:id="@+id/imageViewMyGraphicalImageElement"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="centerCrop">
</ImageView>

And a snippet from the preload code:

sBitmap = bitmapBitmapFactory.decodeResource(context.getResources(), R.drawable.my_graphical_element, options); 
// 'sBitmap' is a Bitmap reference, while 'options' is BitmapFactory.Options

Finally, the place where I set it:

setContentView(R.layout.main);
...
ImageView imageViewMyGraphicalImageElement= (ImageView) findViewById(R.id.imageViewMyGraphicalImageElement);
imageViewMyGraphicalImageElement.setImageBitmap(sBitmap); 

Question: Obviously, the xml-based solution knows about the image before setContentView(...) is called. The preload version sets the image after that call. Is there any difference? Can some autoscaling or other things done by the system be skipped due to this?

like image 967
Thomas Calc Avatar asked Jun 10 '12 20:06

Thomas Calc


2 Answers

I just wrote up an article for this. Wish to be able to answer your question.

https://plus.google.com/112740367348600290235/posts/VNAfFLDcKrw

ImageView has 4 APIs to specify the image. Which one to use? What is the difference?

  1. setImageDrawable(Drawable drawable)
  2. setImageBitmap(Bitmap bm)
  3. setImageResource(int resId)
  4. setImageURI(URI uri)

ImageView, by the name, is used to display an image. But what is a image? A Bitmap is-a image, not hard to understand and we use setImageBitmap for that purpose. However, internally, the ImageView has-a Drawable but not a Bitmap and that is what setImageDrawable for. When you call setImageBitmap, internally, first the bitmap will be wrapped to BitmapDrawable, which IS-A Drawable, and then call setImageDrawable.

Here is the code.

public void setImageBitmap(Bitmap bm) {
    setImageDrawable(new BitmapDrawable(mContext.getResources(), bm));
}

So, what about the 3 and 4 API?

You should already know that that are bunches of ways to create a bitmap, from a file path, from the Uri, or from the resource file.

BitmapFactory.decodeFile(String pathName)
BitmapFactory.decodeStream(Inputstream)
BitmapFactory.decodeResource(Resource res, int id)  
BitmapFactory.decodeByteArray(byte[] data)

Aware of this, it is easy to understand setImageResource/setImageUri is just same as setImageBitmap.

To sum up, setImageDrawable is the primitive function other APIs rely on. The other 3 are just helper methods making you write less code.

In addition, it is very important to keep in mind that ImageView actually has-a Drawable, which not necessarily to be a BitmapDrawable! You could set any Drawable to the Image view.

Besides setting the Drawable through Java API, you could also using XML attribution to set the source Drawable for ImageView. See example below. Note that the shape could be either an image file (.png, .jpg, .bmp) or xml file.

<ImageView
    android:layout_width="match_parent"
    android:layout_height="50dip"
    android:src="@drawable/shape"/>

shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF" android:angle="270"/>
    <padding android:left="7dp" android:top="7dp android:right="7dp" android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>
like image 120
pierrotlefou Avatar answered Sep 26 '22 23:09

pierrotlefou


There is no difference at all. You can consider that all the ImageView constructor does with the android:src attribute is call setImageResource.

Update: Actually it uses setImageDrawable, this is the actual code for ImageView constructor taking attributes:

    Drawable d = a.getDrawable(com.android.internal.R.styleable.ImageView_src);
    if (d != null) {
        setImageDrawable(d);
    }

Reference: ImageView.java

like image 39
K-ballo Avatar answered Sep 24 '22 23:09

K-ballo