Bitmap drawabletobitmap (context ){
Drawable;
// Create a bitmap from the original bitmap
Bitmap bitmap = bitmap
. Createbitmap (
Drawable. getintrinsicwidth (),
Drawable. getintrinsicheight (),
Drawable. getopacity ()! = Pixelformat. opaque? Bitmap. config. argb_8888
: Bitmap. config. rgb_565 );
// Create a canvas with a created bitmap
Canvas canvas = new canvas (Bitmap );
// Retrieve an image from the Resource
Drawable = context. getresources (). getdrawable (imagetype [I]);
// Canvas. setbitmap (Bitmap );
// Set the edges of the drawable image to change the image size.
Drawable. setbounds (0, 0, drawable. getintrinsicwidth (), drawable. getintrinsicheight ());
// Draw the image on the bitmap canvas
Drawable. Draw (canvas );
Return bitmap;
}
In my understanding, we first create a bitmap of the appropriate size based on the original bitmap, then create a canvas, and initialize the canvas with the existing bitmap, then, set the size of the resource image (it is best not to exceed the size of your bitmap, otherwise it will not be completely displayed), and then draw the resource image into a canvas with bitmap, that is over.
(Bitmapdrawable) res. getdrawable (R. drawable. youricon). getbitmap (); in this way, the bitmap is generally an image resource bitmap of the original size without scaling.
Draw bitmap on the canvas to call the function canvas. drawbitmap (bitmap, matrix, mpaint );