XML and setImageDrawable / setImageBitmap

This is beneficial in my application if I preload certain images. I am doing it right in AsyncTask, as it is written in the official documentation. But I have a problem / question about when they should be installed.

I will show the code snippets. Please note that it is simplified (their interoperability is better in my real code, it checks for zeros, etc.).

First look at the original (not preinstalled) version:

<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 (note 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 the preload code snippet:

 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 installed 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 (...). The preload version sets the image after which invokes. Is there any difference? Is it possible to skip some autoscaling or other things that the system does?

+4
source share
2 answers

There is no difference. You can assume that all the ImageView constructor with the android:src attribute is a call to setImageResource .

Update: It actually uses setImageDrawable , this is the actual code for the ImageView constructor that accepts attributes:

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

Link: ImageView.java

+3
source

I just wrote an article for this. I wish you the opportunity to answer your question.

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

ImageView has 4 APIs for specifying an image. Which one to use? What is the difference?

  • setImageDrawable (Drawable drawable)
  • setImageBitmap (bm bitmap)
  • setImageResource (int resId)
  • setImageURI (uri uri)

ImageView , by name, is used to display the image. But what is an image? A Bitmap is an image that is not hard to understand, and for this purpose we use setImageBitmap . However, inside << 20> has-a Drawable , but not a Bitmap , and this is what setImageDrawable for. When you call setImageBitmap , internally, first the bitmap will be wrapped in BitmapDrawable , which is 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 API 3 and 4?

You should already know that these are bundles of ways to create a bitmap from a file path from a Uri or from a resource file.

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

Knowing this, it is easy to understand that setImageResource / setImageUri exactly the same as setImageBitmap .

To summarize, setImageDrawable is a primitive function that other APIs rely on. The other 3 are just helper methods that force you to write less code.

In addition, it is very important to keep in mind that ImageView does have-a Drawable , which does not have to be BitmapDrawable ! You can set any Drawable to the image view.

Besides installing Drawable through the Java API, you can also use XML attribution to set the Drawable source for ImageView . See the example below. Please note: the form can be either an image file (.png, .jpg, .bmp) or an 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> 
+3
source

All Articles