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.
Related
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
I am creating an application in which i have used a method which takes imageView's object as its parameter,but from my code I am getting a canvas object.
So is there any way to convert that canvas object to imageview's object.
So is there any way to convert that canvas object to imageview's
object.
You could convert a canvas to a Bitmap:
Bitmap bitmap = Bitmap.createBitmap((int)width, (int)height, Config.RGB_565));
canvas.setBitmap(bitmap);
And then use that bitmap to be displayed by an ImageView:
imageView.setImageBitmap(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);
}
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
.....
I am facing a problem while decoding an image:
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.smile);
mCanvas = new Canvas(bMap);
as BitmapFactory.decodeResource() returns an immutable object, which we cannot pass to the constructor of Canvas class.
Can I use the function
BitmapFactory.decodeResource(getResources(),Resource, option opt)
as BitmapFactory.options() is used for making the bitmap factory to return only mutable objects, no matter if the image is from "src" or dynamic.
Please help me out to create an image into canvas.
I am loading a bitmap from a resource like so:
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image);
What I want to do is make some changes to the bitmap before It gets drawn to the main canvas in my draw method (As it would seem wasteful to repeat lots of drawing in my main loop when it isn't going to change). I am making the changes to the bitmap with the following:
Canvas c = new Canvas(mBackground);
c.drawARGB(...); // etc
So naturally I get an exception
java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
So to avoid that I made a copy of the bitmap so that it is mutable
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image).copy(Bitmap.Config.ARGB_8888, true);
Which avoid the problem however it sometimes causes OutOfMemoryExceptions, do know any better ways of achieving what I want?
Use decodeResource(Resources res, int id, BitmapFactory.Options opts) and specify inMutable in the options.
http://developer.android.com/reference/android/graphics/BitmapFactory.html
You'd better use RapidDecoder.
import rapid.decoder.BitmapDecoder;
Bitmap mBackground = BitmapDecoder.from(res, R.drawable.image)
.mutable().decode();
Works for API level 8.
Instad of yours:
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image);
Use:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image, options);