Home>

1. Concept difference:

Many netizens have just started to learn the android platform, and the concepts between drawable, bitmap, canvas, and paint are not very clear.In fact, they have already appeared in sun's j2me except drawable.However, in the android platform, bitmap and canvas are all changed.

First let us understand that the display class in the android platform is view, but also provides the underlying graphics class android.graphics. These are the graphics underlying graphics interfaces.

bitmap-called a bitmap

The file format suffix of general bitmap is bmp. Of course, there are many encoders such as rgb565 and rgb888. As a pixel-by-pixel display object, it performs efficiently,However, it is also obvious that the storage efficiency is low.We understand that it is better to store objects.

drawable-As a general-purpose graphic object for Android.

It can load images in common formats,Such as gif, png, jpg, of course, bmp is also supported, and of course some advanced visualization objects are also provided,Such as gradients, graphics, and so on.

canvas-named canvas

We can think of it as a process,Use various methods to manage bitmap, gl, or path. At the same time, it can cooperate with the matrix class to perform operations such as rotation, scaling, etc.The canvas class also provides operations such as cropping and selection.

paint-we can think of it as a drawing tool

Such as paint brushes, paint brushes. He manages the font, color, and style of each drawing tool.

If it involves some android game development and display special effects, you can efficiently implement your own application through these underlying graphics classes.

2. Conversion method:

1) Bitmap is converted to byte

bytearrayoutputstream out=new bytearrayoutputstream ();
bitmap.compress (bitmap.compressformat.png, 100, out);
byte [] array=out.tobytearray ();

2) byte into bitmap

final contentresolver contentresolver=context.getcontentresolver ();
final packagemanager manager=context.getpackagemanager ();
final cursor c=contentresolver.query (uri, null, null, null, null);
final int icon3dindex=c.getcolumnindexorthrow (columnname);
byte [] data=c.getblob (icon3dindex);
bitmap bitmap=bitmapfactory.decodebytearray (data, 0, data.length);

3) bitmap conversion drawable

bitmap bitmap=new bitmap (...);drawable drawable=new bitmapdrawable (bitmap);
//drawable drawable=new fastbitmapdrawable (bitmap);

4) drawable to bitmap

a. bitmapdrawable, fastbitmapdrawable directly using getbitmap

b. Other types of drawables are drawn to a bitmap using canvas.

canvas canvas=new canvas (bitmap)
  drawable.draw (canvas);
drawable d=imageslist.get (0);bitmap bitmap=((bitmapdrawable) d) .getbitmap ();
  • Previous jQuery implements announcement news auto scroll effect example code
  • Next Detailed single and multiple action examples in Struts