What is the difference between Bitmap and Drawable in Android?

I googled, but I could not find any article to describe the difference between Bitmap and Drawable in Android.

+66
android terminology android-bitmap android-drawable
Jan 20 '12 at 5:14
source share
4 answers

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

Most Android UIs like working with Drawable objects, not Bitmap. A View can accept any Drawable as background. ImageView can display Drawable foreground. Images stored as resources are loaded as paintable objects.

+65
Jan 20 2018-12-12T00:
source share

Drawable is what you can draw. For example. layout, vector image (line, circle), font, image, etc.

Bitmap is a certain type of Drawable, which is an image, for example: PNG, JPEG or so

+36
Jan 20 '12 at 5:22
source share

A bitmap is not an image. A bitmap is a bitmap (note: Bitmap). And this map represents pixels on which you can draw something. It could be your own bitmap (not an image), like a square:

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

or you can create a raster object from an image:

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

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

Everything about Drawable is well described above.

TL; DR

Some people write what you draw on canvas. You do not draw on canvas. You draw on raster pixels using the 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 
+18
Jun 03 '18 at 3:33
source share

Resource <Resources>

Resource

A Drawable is a general concept of a graphic object that can be displayed on the screen and which you can get using an API, for example getDrawable(int) , or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawings:

  • A bitmap file, a bitmap image file (.png, .jpg or .gif), creates a BitmapDrawable .

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

  • A list of layers, A Drawable , which manages an array of other Drawable s. They are drawn in array order, so the element with the highest index is drawn above, creates a LayerDrawable .

  • A list of states, an XML file that refers to different raster graphics for different states (for example, to use a different image when a button is clicked) creates a StateListDrawable .

  • The list of levels, XML , which defines Drawable , which manages several alternative Drawable s, each of which assigns a maximum numerical value, creates a LevelListDrawable .

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

  • Drawable insert, an XML file that defines a Drawable that inserts another Drawable at a specified distance. This is useful when a View needs a background Drawble that is smaller than viewing the actual borders.

  • Clip Drawable, an XML file that defines a Drawable that will copy another Drawable based on this value of the current Drawable level, creates a ClipDrawable .

  • A scalable Drawable, an XML file that defines a Drawable that resizes 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 creating an AnimationDrawable .

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

Raster

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

You can directly reference the bitmap file using the file name as the resource identifier or create an alias resource identifier in XML.

Note. Raster files can be automatically optimized during lossless compression using the aapt tool during the build process. For example, PNG with a true color that does not require more than 256 colors can be converted to an 8-bit PNG with a color palette. This will result in image quality, but less memory is required. Therefore, be aware that binary image files located in this directory may change during assembly. If you plan to read the image as a bitstream to convert it to a bitmap, put the images in the res/raw/ folder where they will not be optimized.

+12
Mar 28 '15 at 16:13
source share



All Articles