Basic operations of drawable and Bitmap, drawablebitmap
1. drawable graphical objects. Images in common formats can be reprinted, which may be Bitmapdrawable, shapedrawable, or GIF, PNG, or JEPG.
2. Bitmap is a Bitmap for image processing.
3. Canvas indicates the Canvas, which is the target area of the painting. It is used to manage Bitmp or path.
The following describes how to convert drawable to Bitmap.
I know there are two methods (two methods: good and bad, depending on the situation)
(1) A common method is to create a Bitmap, bind the Bitmap to the canvas, and draw the drawable to bitmap (this method is used when the drawable object is not a Bitmap image)
Bitmap bitmap = Bitmap. createBitmap (size, size, Bitmap. config. ARGB_8888); // create a Canvas canvas Canvas = new Canvas (bitmap) with a high size and width; // bind the canvas to the bitmap to prepare drawable for the next drawable painting. setBounds (, size, size); // drawable sets the same size as the bitmap drawable. draw (canvas); // draw drawable to Bitmap through canvas
(2) Another method is to directly obtain Bitmap (bitmapdrawable)
Bitmap icon = BitmapFactory. decodeResource (context. getResources (), R. drawable. icon_resource) // convert the icon_resource image in the drawable object to the icon of the Bitmap file
Or
BitmapDrawable bd = (BitmapDrawable) drawable; // first converts it to Bitdrawable bitmap = bd. getBitmap (); // then obtains Bitmap through getBitmap ().
Description of the drawmap () method
DrawBitmap () method: draws an image. This method is used to draw an image on the canvas through a Bitmap object. This method can be used to introduce image resources.
DrawBitmap (Bitmap bitmap, float left, float top, painting)
Parameter description
Bitmap: a Bitmap object that represents image resources.
Left: left position of the image.
Top: the top position of the image.
Paint: the paint brush used for painting.
This method is used in method 1 above.
Public class MyTile extends View {public Bitmap [] bitmap1; private static final int size = 12; public MyTile (Context context, AttributeSet attrs) {super (context, attrs ); get_Bitmap (); // The constructor stub automatically generated by TODO}/* (non-Javadoc) * @ see android. view. view # onDraw (android. graphics. canvas) */public void Get_Bitmap () {bitmap1 = new Bitmap [1]; Bitmap bitmap = Bitmap. createBitmap (size, size, Config. ARGB_8888); // create a new size-square bitmap Drawable drawable = getResources (). getDrawable (R. drawable. greenstar); // obtain the drawable object Canvas canvas = new Canvas (bitmap); // bind the Bitmap to the Canvas drawable. setBounds (, size, size); // you can specify the size of a drawable object. draw (canvas); // Finally, draw drawable to the Bitmap canvas. bitmap1 [0] = bitmap;} protected void onDraw (Canvas canvas) {// automatically generated method stub super. onDraw (canvas); Paint paint = new Paint (); paint. setColor (Color. BLACK); canvas. drawBitmap (bitmap1 [0], 200,200, paint );}}
The above method can determine the size and size of an image. This method is usually used when the size of a custom bitmap is used.