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

后端 未结 4 1307
故里飘歌
故里飘歌 2020-12-05 14:10

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

Why there two methods?

4条回答
  •  心在旅途
    2020-12-05 14:53

    The method setBackgroundResource() belongs to all Views. The method setImageResource() only belongs to ImageView. You can set them both:

    imageView.setBackgroundResource(R.drawable.sky);
    imageView.setImageResource(R.drawable.balloons);
    

    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.

    My fuller answer is here.

提交回复
热议问题