Could anyone tell me what is the difference between setBackgroundResource(resourceid)
and setBackgroundDrawable(getResource().getDrawable(drawableid))
in android?
To use an image resource, add your file to the res/drawable/ directory of your project. Once in your project, you can reference the image resource from your code or your XML layout. Either way, it's referred to using a resource ID, which is the file name without the file type extension. For example, refer to my_image.
A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.
You can have a look at the Android source code for the View class yourself and find out that there is very little difference!
public void setBackgroundResource(int resid) {
if (resid != 0 && resid == mBackgroundResource) {
return;
}
Drawable d= null;
if (resid != 0) {
d = mResources.getDrawable(resid);
}
setBackground(d);
mBackgroundResource = resid;
}
And setBackground()
just calls through to setBackgroundDrawable()
...
public void setBackground(Drawable background) {
//noinspection deprecation
setBackgroundDrawable(background);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With