Luk*_*kap 17 android background image imageview
我在设置图像时看到了这些不同的方法,但我没有区别.
为什么有两种方法?
Dim*_*ris 42
setBackgroundResource用于设置ImageView的背景.
setImageResource用于设置ImageView的src图像.鉴于:
ImageView iv = new ImageView(this);
Run Code Online (Sandbox Code Playgroud)
然后:
iv.setBackgroundResource(R.drawable.imagedata);
Run Code Online (Sandbox Code Playgroud)
适合整个背景的图像.这意味着即使图像尺寸太小,它也会拉伸图像以完全填充背景.
imageView.setImageResource(R.drawable.imagedata);
Run Code Online (Sandbox Code Playgroud)
将仅占用ImageView中图像的大小.为此你想要设置
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Run Code Online (Sandbox Code Playgroud)
为您的ImageView.如果图像的尺寸小于ImageView,则剩余的边框将留空,并显示背景.