Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

android - What is the difference between ImageView.setBackgroundResource and ImageView.setImageResource?

I have seen these different approaches in setting images but I don't get the difference.

Why there two methods?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

android:layout_width="wrap_content"
android:layout_height="wrap_content"

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...