Reputation: 3305
I have my application that is displaying images with different ratio, resized inside (centerInside) imageView. What I need is to create bitmap from the ImageView including the background (black in this case).
So for example I have device screen 320x480, full screen imageView with image resized to 280x480. How could I get 320x480 bitmap from it?
On top of this imageview I have some logos or buttons that I don't want to include to bitmap, they're like on top layer. All I need is bitmap with image and black border from some sides.
Upvotes: 44
Views: 131623
Reputation: 12469
As buildDrawingCache()
is deprecated, From this SO answer, we need to get bitmap directly from any android View.
Example:
fun getBitmapFromView(view: View): Bitmap {
val bitmap = Bitmap.createBitmap(
view.width, view.height, Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
view.draw(canvas)
return bitmap
}
fun getBitmapFromView(view: View, defaultColor: Int): Bitmap {
val bitmap = Bitmap.createBitmap(
view.width, view.height, Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
canvas.drawColor(defaultColor)
view.draw(canvas)
return bitmap
}
Use like this way:
//@param rootView is View object which you want to get bitmap
Bitmap bitmap = getBitmapFromView(rootView);
Bitmap bitmapColored = getBitmapFromView(rootView, Color.WHITE);
Upvotes: 1
Reputation: 1674
In Kotlin;
imageView.drawable?.let {
val mBitmap = (it as BitmapDrawable).bitmap
}
Upvotes: 0
Reputation: 1
It works in Kotlin after buildDrawingCache()
being deprecated
// convert imageView to bitmap
val bitmap = (imageViewId.getDrawable() as BitmapDrawable).getBitmap()
Upvotes: 0
Reputation: 295
This is a working code
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
Upvotes: 2
Reputation: 21
try {
photo.setImageURI(Uri.parse("Location");
BitmapDrawable drawable = (BitmapDrawable) photo.getDrawable();
Bitmap bitmap = drawable.getBitmap();
bitmap = Bitmap.createScaledBitmap(bitmap, 70, 70, true);
photo.setImageBitmap(bitmap);
} catch (Exception e) {
}
Upvotes: 2
Reputation: 10184
You could just use the imageView's image cache. It will render the entire view as it is layed out (scaled,bordered with a background etc) to a new bitmap.
just make sure it built.
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
there's your bitmap as the screen saw it.
Upvotes: 98
Reputation: 134664
Just thinking out loud here (with admittedly little expertise working with graphics in Java) maybe something like this would work?:
ImageView iv = (ImageView)findViewById(R.id.imageview);
Bitmap bitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
iv.draw(canvas);
Out of curiosity, what are you trying to accomplish? There may be a better way to achieve your goal than what you have in mind.
Upvotes: 4
Reputation: 200090
Have you tried:
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Upvotes: 79