下面是兩個官方的描述:
android:background setBackgroundResource(int) A drawable to use as the background.
android:src setImageResource(int) Sets a drawable as the content of this ImageView.
有下面幾個不同點:
1.src是前景foreground。background是後景background。
2.src是顯示内容,background是背景。
3.background是所有view都有的屬性,而src是ImageView特有的,它會受到android:scaleType的影響,而background總是縮放到view的大小。
問題延伸:
一、ImageView設定background和src的差別。
1.src是圖檔内容(前景),bg是背景,可以同時使用。
2.background會根據ImageView元件給定的長寬進行拉伸,而src就存放的是原圖的大小,不會進行拉伸 。
3.scaleType隻對src起作用;bg可設定透明度。
二、ImageView幾種不同的設定圖檔的方式。
設定background:
1.image.setBackground(getResources().getDrawable(R.drawable.blackk));//變形
2.image.setBackgroundResource(R.drawable.blackk);//變形 3.image.setBackgroundDrawable(getResources().getDrawable(R.drawable.blackk));變形
源碼:這三種方法的實質都是調用方法3setBackgroundDrawable()。
設定src:
1.image.setImageDrawable(getResources().getDrawable(R.drawable.blackk)); //不會變形
2.Stringpath=Environment.getExternalStorageDirectory()+File.separator+”test1.jpg”;
Bitmap bm = BitmapFactory.decodeFile(path);
image.setImageBitmap(bm);//不會變形
3.image.setImageResource(R.drawable.blackk);//不會變形
源碼: 其中方法2就是将bitmap轉換為drawable然後調用方法1,方法1和方法3都是調用updateDrawable()方法。