What is the difference between Bitmap and Drawable in Android?

60,923

Solution 1

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

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

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

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.

Share:
60,923

Related videos on Youtube

Nikhil C George
Author by

Nikhil C George

Updated on July 15, 2022

Comments

  • Nikhil C George
    Nikhil C George almost 2 years

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

    • Jrom
      Jrom over 12 years
      This solution should be able to answer your questions: solution
  • Ted Hopp
    Ted Hopp over 12 years
    A Bitmap (android.graphics.Bitmap) is not a subclass of Drawable (android.graphics.drawable.Drawable).
  • Barmaley
    Barmaley over 12 years
    Sure, it's my fault. I meant BitmapDrawable
  • android developer
    android developer almost 12 years
    if a bitmapDrawable wraps a bitmap , what would occur if we call recycle() on the bitmap within it ? also , what does recycle() do for API11 and above?
  • Ted Hopp
    Ted Hopp almost 12 years
    @androiddeveloper - I'm not sure, but it won't be anything good. Once a bitmap is wrapped by a BitmapDrawable, the drawable "owns" the bitmap and is supposed to manage its life. If you call recycle(), you are interfering with that relationship. At best, the drawable will draw nothing; more likely, some sort of exception will be thrown later on. According to the docs, recycle() with API11+ does the same thing it always did: it allows you to free the native pixels as soon as you know you are done with them.
  • android developer
    android developer almost 12 years
    they say there that it frees the native memory of the bitmap , but we know that as of API 11 , everything related to the bitmap (including its raw data) is inside the heap , so recycle seems like a weird thing to do ...
  • Ted Hopp
    Ted Hopp almost 12 years
    @androiddeveloper - Looking at the source, it seems that there's still a native bitmap. For performance purposes, the raw buffer is exposed as a public member (although still hidden from the public API). Calling recycle() will still free the native bitmap; it will also set the member buffer to null.
  • android developer
    android developer almost 12 years
    yes, i've read it too , but it doesn't make sense, since they said on google IO that the bitmap is no longer stored in the native memory . i'm really confused of how could it be.
  • HarshitMadhav
    HarshitMadhav almost 6 years
    Nice answer deserves +1 :) Thanks for the explaining by lines of code.
  • mallaudin
    mallaudin over 5 years
    But they say Bitmap: the simplest Drawable, a PNG or JPEG image. in docs.
  • mallaudin
    mallaudin over 5 years
    I thought I pasted from this link. developer.android.com/guide/topics/resources/…
  • Tushar Srivastava
    Tushar Srivastava over 5 years
    perfectly explained