How To Create Bitmap Form Drawable Object
I am developing custom view for android. For that I want give a user ability to select and image using just like when using ImageView In attr.xml I added bellow code.
Solution 1:
You can convert your Drawable
to Bitmap
like this (for resource):
Bitmapicon= BitmapFactory.decodeResource(context.getResources(),
R.drawable.drawable_source);
OR
If you've it stored in a variable, you can use this :
publicstatic Bitmap drawableToBitmap(Drawable drawable) {
Bitmapbitmap=null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawablebitmapDrawable= (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvascanvas=newCanvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
Post a Comment for "How To Create Bitmap Form Drawable Object"