Difference between setImageResource and setImageDrawable

3

To set an image in ImageView I did the following using setImageDrawable :

imagem.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.cliente));

When searching ImageView, I also found the setImageResource method and it also worked like this:

 imagem.setImageResource(R.drawable.cliente);

But I did not find out the difference between the methods and what should I use in that situation, depending on an information from another activity the method arrow the corresponding image.

    
asked by anonymous 15.11.2016 / 17:11

2 answers

3

The purpose of the two methods is the same: assign the content to ImageView , the difference is only in the type that each of the methods receives.

In addition to these, ImageView provides other methods with the same purpose and that receive other types:

What to use will depend on what type is available at the time.

By what is implied is a Resource Id that is available, then imagem.setImageResource(R.drawable.cliente); must be used. In addition, since it is a int , it is easy to pass it to another Activity, as appears to be the case.

    
15.11.2016 / 18:20
1

The way you're doing, none, because you're converting a resource into a Drawable. But Drawables need not come from Resources: they can come from local files of the device, from the Internet, from an XML, can be generated by some algorithm etc.

In short: setImageResource is for images that are in their resources and setImageDrawable is for any Drawable, from wherever it came from.

    
15.11.2016 / 17:28