What is the difference between Bitmap and Drawable in Android?

AndroidAndroid DrawableTerminologyAndroid Bitmap

Android Problem Overview


I googled but i couldn't find any article to describe about the difference between Bitmap and Drawable in Android.

Android Solutions


Solution 1 - Android

A Bitmap is a representation of a bitmap image (something like java.awt.Image). A Drawable is an abstraction of "something that can be drawn". It could be a Bitmap (wrapped up as a BitmapDrawable), but it could also be a solid color, a collection of other Drawable objects, or any number of other structures.

Most of the Android UI framework likes to work with Drawable objects, not Bitmap objects. A View can accept any Drawable as a background. An ImageView can display a foreground Drawable. Images stored as resources are loaded as Drawable objects.

Solution 2 - Android

Bitmap is not an image. Bitmap is a map of bit (note name: Bit-map). And this map represents pixels on which you can draw something. It may be your own custom bitmap (not image), for example square:

Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

or you can create Bitmap object from image:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.your_image);    

A Bitmap is a pixels holder. And Canvas is used to draw something on your bitmap (on Bitmap pixels).

Everything about Drawable is well described above.

TL;DR

Some people write that you draw on Canvas. You don't draw on Canvas. You draw on Bitmap pixels with Canvas helper method.

Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.RED) // now all bitmap pixels became red

Solution 3 - Android

Drawable is something which can be drawn. E.g. layout, vector image (line, circle), font, image and so on

Bitmap - is specific type of Drawable which is image, like: PNG, JPEG or so

Solution 4 - Android

Drawable Resource

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, A Bitmap graphic file, (.png, .jpg, or .gif), creates a BitmapDrawable.

  • Nine-Patch File, A PNG file with stretchable regions to allow image resizing based on content (.9.png), creates a NinePatchDrawable.

  • Layer List, A Drawable that manages an array of other Drawables. These are drawn in array order, so the element with the largest index is drawn on top, creates a LayerDrawable.

  • State List, An XML file that references different bitmap graphics for different states (for example, to use a different image when a button is pressed), creates a StateListDrawable.

  • Level List, An XML file that defines a Drawable that manages a number of alternate Drawables, each assigned a maximum numerical value, creates a LevelListDrawable.

  • Transition Drawable, An XML file that defines a Drawable that can cross-fade between two Drawable resources, creates a TransitionDrawable.

  • Inset Drawable, An XML file that defines a Drawable that insets another Drawable by a specified distance. This is useful when a View needs a background Drawble that is smaller than the View's actual bounds.

  • Clip Drawable, An XML file that defines a Drawable that clips another Drawable based on this Drawable's current level value, creates a ClipDrawable.

  • Scale Drawable, An XML file that defines a Drawable that changes the size of another Drawable based on its current level value, creates a ScaleDrawable.

  • Shape Drawable, An XML file that defines a geometric shape, including colors and gradients, creates a ShapeDrawable.

Also see the Animation Resource document for how to create an AnimationDrawable.

Note: A color resource can also be used as a Ddrawable in XML. For example, when creating a StateListDrawable, you can reference a color resource for the android:drawable attribute (android:drawable="@color/green").

Bitmap

A bitmap image. Android supports bitmap files in three formats: .png (preferred), .jpg (acceptable), .gif (discouraged).

You can reference a bitmap file directly, using the filename as the resource ID, or create an alias resource ID in XML.

Note: Bitmap files may be automatically optimized with lossless image compression by the aapt tool during the build process. For example, a true-color PNG that does not require more than 256 colors may be converted to an 8-bit PNG with a color palette. This will result in an image of equal quality but which requires less memory. So be aware that the image binaries placed in this directory can change during the build. If you plan on reading an image as a bit stream in order to convert it to a bitmap, put your images in the res/raw/ folder instead, where they will not be optimized.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNikhil C GeorgeView Question on Stackoverflow
Solution 1 - AndroidTed HoppView Answer on Stackoverflow
Solution 2 - AndroidbitvaleView Answer on Stackoverflow
Solution 3 - AndroidBarmaleyView Answer on Stackoverflow
Solution 4 - AndroidMisbah FarooqiView Answer on Stackoverflow