The setBackgroundResource() method will cause the image's width and height will be stretched to fill the size of the view. The setImageResource() method will let its image keep its aspect ratio.
SetBackdroundResource is for a drawable or color you want to set at the background of the imageview and your setImageResource is like to display on it.
so setImageResource is for add any resource to your imageview's front side. try this example and look at the difference. Android Gallery, ImageView Example
. This is a two layer effect,backside (setBackgroundResource) and frontside (setImageResource).
setBackgroundResource is for setting the background of an ImageView. setImageResource is for setting the src image of the ImageView.
Given:
ImageView iv = new ImageView(this);
Then:
iv.setBackgroundResource(R.drawable.imagedata);
Will fit the image for the entire background. That means it will stretch the image to fill that background entirely even if the image size is too small.
imageView.setImageResource(R.drawable.imagedata);
Will occupy only the size of the image in ImageView.
For that you want to also set
The method
setBackgroundResource()
belongs to all Views. The methodsetImageResource()
only belongs toImageView
. You can set them both:The
setBackgroundResource()
method will cause the image's width and height will be stretched to fill the size of the view. ThesetImageResource()
method will let its image keep its aspect ratio.My fuller answer is here.
setBackgroundResource
sets the background image of an ImageView. The XML attribute is:android:background
setImageResource
sets the image displayed in an ImageView. The XML attribute is:android:src
SetBackdroundResource is for a drawable or color you want to set at the background of the imageview and your setImageResource is like to display on it.
so setImageResource is for add any resource to your imageview's front side. try this example and look at the difference. Android Gallery, ImageView Example . This is a two layer effect,backside (setBackgroundResource) and frontside (setImageResource).
setBackgroundResource
is for setting the background of an ImageView.setImageResource
is for setting the src image of the ImageView. Given:Then:
Will fit the image for the entire background. That means it will stretch the image to fill that background entirely even if the image size is too small.
Will occupy only the size of the image in ImageView. For that you want to also set
for your ImageView. If the size of the image is smaller than the ImageView the remaining border will be left blank and the background will be shown.