Kirill
Kirill

Reputation: 1472

Clear Bitmap in 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);

Upvotes: 8

Views: 23409

Answers (3)

Hiren Patel
Hiren Patel

Reputation: 52800

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.

Upvotes: 7

Kirill
Kirill

Reputation: 1472

The solution was to use

bitmap.eraseColor

Upvotes: 4

Jibran Khan
Jibran Khan

Reputation: 3256

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

Upvotes: 37

Related Questions