Christopher Francisco
Christopher Francisco

Reputation: 16268

Can a bitmap point to another bitmap?

I have a bitmap downloader class which decodes a stream into a Bitmap object.

Can I do something like

Bitmap bitmap;

bitmap = object;

when I do imageview.setImageBitmap(bitmap) would be the same as imageview.setImageBitmap(object)

Also, is it posible to create multiple instances of bitmap? like:

for(i = 0; i < 10; i++) { 
    Bitmap bitmap = new Bitmap();  // how to do this? 
    new BitmapDownloaderAsynctask(bitmap).execute(url); 
}

Upvotes: 0

Views: 98

Answers (1)

Pavel Dudka
Pavel Dudka

Reputation: 20934

when I do imageview.setImageBitmap(bitmap) would be the same as imageview.setImageBitmap(object)

Yes, it would be the same (as long as object is another instance of Bitmap)

In order to create Bitmap manually, there is a bunch of static methods Bitmap.createBitmap() exist to create bitmaps (Bitmap class)

As an example, here is the easiest way to create a bitmap:

Bitmap bmp = Bitmap.createBitmap(100, 100, Config.ARGB_8888); //100*100 bitmap in ARGB color space

EDIT:

If you need to keep bitmap reference unchanged, you need to decode stream in a separate bitmap and then copy content of this bitmap into your original bitmapHolder. You can do that by drawing on the canvas:

AsyncTask code:

.....

Canvas canvas = new Canvas(bitmapHolder); //this bitmap was passed to AsyncTask
Bitmap tmpBitmap = Bitmap.decodeStream(...);

canvas.drawBitmap(tmpBitmap, 0, 0, null); //this will copy your decoded bitmap into your original bitmap which was passed into AsyncTask

.....

Upvotes: 1

Related Questions