Clear Bitmap in Android

I am creating my first chart using a bitmap and canvas. How can I clear the bitmap to draw a new graph?

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);

      

+3


source to share


3 answers


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 the bitmap memory below the code

bitmap.recycle();

      



The bitmap recycle () method reference as per this .

public void recycle () Added in API Level 1 Free the native object associated with this bitmap and clear the pixel data reference. It does not free the pixel data synchronously; it just allows garbage collection if there are no other links. The bitmap is marked dead, which means it will throw an exception if getPixels () or setPixels () is called and does not draw anything. This operation cannot be undone, so you should only call it when you are sure that the bitmap is no longer being used. This is an extended call and generally does not need to be called as the normal GC process will free this memory if there are no more references to this bitmap.

+4


source


You can use eraseColor

in a bitmap to set its color to Transparent. It will be used again without recreating it.

bitmap.eraseColor(Color.TRANSPARENT);

      



Further reading here

+14


source


The solution was to use

bitmap.eraseColor

      

+3


source







All Articles