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);
Related
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.
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 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'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.
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.