Clear Bitmap in Android - android

I create first chart using bitmap and canvas. How I can clear bitmap for drawing new chart?
ImageView imageView = new ImageView(this);
Bitmap bitmap = Bitmap.CreateBitmap(w, h, Bitmap.Config.Argb8888);
Canvas canvas = new Canvas(bitmap);
...
imageView.SetImageBitmap(bitmap);
relativeLayout.AddView(imageView);

You can use eraseColor on bitmap to set its color to Transparent. It will useable again without recreating it.
bitmap.eraseColor(Color.TRANSPARENT);
Further reading here

ImageView imageView = new ImageView(this);
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
// Your Other code
imageView.setImageBitmap(bitmap);
relativeLayout.AddView(imageView);
Now release memory of bitmap by below code
bitmap.recycle();
Help of recycle() method of bitmap as per this.
public void recycle ()
Added in API level 1
Free the native object associated with this bitmap, and clear the reference to the pixel data. This will not free the pixel data synchronously; it simply allows it to be garbage collected if there are no other references. The bitmap is marked as "dead", meaning it will throw an exception if getPixels() or setPixels() is called, and will draw nothing. This operation cannot be reversed, so it should only be called if you are sure there are no further uses for the bitmap. This is an advanced call, and normally need not be called, since the normal GC process will free up this memory when there are no more references to this bitmap.

The solution was to use
bitmap.eraseColor

Related

How to know that Canvas has used bitmap object

Hi I am creating Bitmap object and using in canvas, like below code
Bitmap result = Bitmap.createBitmap(loadedImage.getWidth(), loadedImage.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
mCanvas.drawBitmap(blurBitmap, 0, 0, null);
mCanvas.drawBitmap(loadedImage, 0, 0, paint);
paint.setXfermode(null);
ivBlur.setImageBitmap(result);
// now i don't want Bitmap "result" so I am recycling it
if (result != null && !result.isRecycled())
result.recycle();
}
But from above code I am getting an error like
trying to use recycled bitmap android
How to fix "canvas: trying to use a recycled bitmap error"?
trying to use a recycled bitmap android.graphics.Bitmap
Canvas: trying to use a recycled bitmap android.graphics.Bitmap in android
If I don't write last three lines it is working perfect,
It may be because Canvas is using Bitmap "result" object and I am recycling "result"
Because of this bitmap is taking huge space, I have to recycle this bitmap otherwise I will get
java.lang.OutofMemoryError: bitmap size exceeds VM budget.
So does anyone know how can I get that, "Canvas has used bitmap object and now I can recycle result object".
The issue isn't the the Canvas has used the bitmap. The issue is that you passed the Bitmap to an ImageView that needs it to draw itself Once you pass that Bitmap to an ImageView, you can't recycle it for as long as the ImageView is alive.
You can recycle the bitmap only after the ImageView with the Bitmap is removed from the view hierarchy and never used again. In that case, it's easier to just let the garbage collector handle it.
If you instead need a smaller Bitmap, you should create and draw into that smaller Bitmap before sending it to the ImageView.

Cannot draw on bitmap loaded canvas

I am trying to create an image save and retrieve feature in android. The code I have for creating a jpg file from canvas is as below
Bitmap bitmap = Bitmap.createBitmap( view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
I am trying to read the jpg file and create image on canvas using
Bitmap bMap = BitmapFactory.decodeStream(buf);
Bitmap workingBitmap = Bitmap.createBitmap(bMap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
view.mybitmap = mutableBitmap;
view.onDraw(view.Canvas);
The code I have in onDraw is
canvas.drawBitmap(view.mybitmap, 0, 0, view.myPaint);
This draws the bitmap on canvas correctly from the stored jpg file but I am not able to draw anything on the canvas after that. Has it loaded a immutable bitmap image on canvas which I am not able to edit?
Any help will be appreciated! Thanks!
You shouldn't need to be calling the ondraw method directly like you are doing currently. For a custom view class all you should need to be calling is view.invalidate(); This will auto call the ondraw method and re-draw the canvas. You will probably need to update the logic in your ondraw method to handle the cases where you want to draw additional things on the canvas.

Setting a bitmap using RemoteView not working

Im writing a widget and I need to download and set a bitmap on the layout. Everything I've tried doesn't seem to work.
I've created a test bitmap now to set on the view, [update] this works.
Bitmap.Config config = Bitmap.Config.ARGB_8888;
Bitmap bitmap = Bitmap.createBitmap(imageActiveWidth, imageHeight, config);
Canvas canvas = new Canvas(bitmap); // Load the Bitmap to the Canvas
Paint paint = new Paint();
paint.setColor(0xFFFFCCFF);
canvas.drawRect(0, 0, imageActiveWidth, imageHeight, paint);
views.setImageViewBitmap(resId, bitmap);
using a resource file does work:
Bitmap placeholderBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.placeholder_medium);
views.setImageViewBitmap(imageSlotId, placeholderBitmap);
However using a downloaded bitmap does not seem work.
(after async task has downloaded bitmap, I have a method setBitmap which is one line:
views.setImageViewBitmap(resId, proxy);
Result - screen is just white, no bitmap
I'm really stumped on how to get this to work, because I need to be able to download bitmaps and set them.
Found a solution. I think its related to this bug:
http://code.google.com/p/android/issues/detail?id=8489
Solved by changing by setBitmap method to the following:
private void setBitmap(RemoteViews views, int resId, Bitmap bitmap){
Bitmap proxy = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(proxy);
c.drawBitmap(bitmap, new Matrix(), null);
views.setImageViewBitmap(resId, proxy);
}
And I needed to call:
AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, views);
AFTER the bitmaps had been set.
For some reason this wasn't working when I came back to it. My view was an AdapterViewFlipper, so i used the above method with a call to widgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.content); which caused the bitmaps to render.

Modifying Bitmap from resources

I'm trying to draw some addition to my ImageView's content, which is represented by Drawable from resources. I know that all Drawables from resources are immutable by default, but even after I call mutable() method and get Bitmap from this Drawable to pass it to Canvas object I get java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor. Here is the code:
Drawable homeImage = mHomeImage.getBackground(); // my ImageView
homeImage.mutate();
Canvas c = new Canvas(((BitmapDrawable)homeImage).getBitmap());
Is there any way to modify this Bitmap without creating it's copy?
Try this
Bitmap bmp1 = (((BitmapDrawable)homeImage).getBitmap())
.copy(Config.ARGB_8888, true);
Canvas c = new Canvas(bmp1);
You will need to create a new bitmap for the canvas. As the canvas uses the bitmap we pass to store whats drawn, the bitmap should be mutable.

Print Bitmaps onto other bitmap android

I am trying to make my game a bit easier on the phone, so I am trying to figure out a way to print a bunch of bitmaps onto another big one, so I can just do it once, rather than every time the screen is redrawn. So, is there any way to do this? I know there is a way to print everything that is printed to the canvas to a bitmap, but I can't seem to get that to work. If that is the only way can someone explain how to do that? Thanks in advance.
Here is something I tried, but it didn't work out so well
Bitmap background;
Canvas canvas;
private void methodName() {
background = Bitmap.createBitmap(width, height, someKindOfConfigThing);
canvas = new Canvas(background);
canvas.drawBitmap(blahblah);
}
What you would do is to create the main bitmap, attach that to a canvas to which you can draw.
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
Canvas c = new Canvas(bitmap);
You can draw (parts of) bitmaps to this canvas using
c.drawBitmap(anotherBitmap, transformMatrix, paint);
To attach the main bitmap to the view you would create a new ImageView, call setImageBitmap passing your main bitmap and set it as the current contentview using setContentView.
If you want to combine multiple bitmaps to another big one and reuse that, you already on the right way! Show us what you have done and tell us what the result is. I guess we can help you :)
[update] it should be possible to save this new bitmap to disc or store it temporarily as a variable:
private void methodName() {
background = Bitmap.createBitmap(width, height, someKindOfConfigThing);
canvas = new Canvas(background);
// drawing on the canvas should change the bitmap "background" too
canvas.drawBitmap(blahblah);
FileOutputStream fos = null;
try {
fos = new FileOutputStream("/path/to/image.png");
background.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
// catching...
}
}

Categories

Resources