How to clean up a bitmap? - android

I'm creating a new empty transparent Bitmap and making some modifications to it.
Is there a way to clean up this (make transparent and empty again) without creating new instance of Bitmap?
UPDATE:
Thanks for your help.
I have found answer.
// Fills the bitmap's pixels with the specified Color.
bitmap.eraseColor(somecolor);

Try this:
myBitmap.eraseColor(android.graphics.Color.TRANSPARENT);
The constant int value of TRANSPARENT is 0x00000000.

Your Bitmap must be a mutable Bitmap.
when your bitmap like this
Bitmap bitmap=.......
then convert it into mutable bitmap by using below
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
then
mutableBitmap.eraseColor(somecolor);

Related

Convert BitmapDrawable to Bitmap

In my project a bitmap gets filled with text and another bitmap, the method to add the text to the bitmap returns a BitmapDrawable.
I want to save this BitmapDrawable as a pdf file, so I can email the original bitmap, with text and other image(s) added.
For this, I'll need a regular Bitmap instead of a BitmapDrawable.
I can't seem to find any answers on this, because I don't have a reference to a drawable in a drawable folder, I just have a BitmapDrawable to work with.
Any ideas on this?
BitmapDrawables support a getter for the bitmap.
Bitmap bitmap = bitmapDrawable.getBitmap();

create part of bitmap as a new bitmap

i have a problem and i don't know how to solve it. What i'm trying to do is, to create a Bitmap which is a part of another Bitmap.
I tried already this
Bitmap newBitmap = Bitmap.createBitmap(bitmap, (int)x, (int) y, (int)width, (int)height);
The width/height of the new bitmap is alright, but it's empty, every pixel is just transparent.
So i searched and found that i have to use canvas to draw the part again, because the return of createBitmap is a empty once. But i'm not sure since the call up here returns an immutable Bitmap, which Canvas doesn't like. I also not know which constructor i have to use with canvas.
Could someone help me?
Thanks and greetings
That one will create an INMUTABLE bitmap. Please, refer to http://developer.android.com/reference/android/graphics/Bitmap.html and read about that method.
To create a mutable one, use createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config). Pass in the widht and height of the part u wanna render in and the displayMetrics you get from yourActivity.getResources().getDisplayMetrics().
To render the area you want in the newly created bitmap, yes, use the Canvas. There are lots of methods. You can use this one: http://developer.android.com/reference/android/graphics/Canvas.html#drawBitmap(android.graphics.Bitmap, android.graphics.Rect, android.graphics.RectF, android.graphics.Paint)
You pass in a rect with the source area and the destination area.

Set Picture into RemoteViews

I have some SVGs in my assets folder and I need to dynamically set them in my widget (on an ImageView).
I am using this library: http://code.google.com/p/svg-android/
This library returns a Picture or a PictureDrawable.
The only methods I can see to use on RemoteViews are setImageViewBitmap which obviously takes a bitmap.
I tried looking for code to convert a Drawable to a Bitmap like this:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
currentBitmap = bitmap;
But the bitmap is too small. When I create the bitmap in Illustrator I set the artboard size to 65 which is what comes through on the intrinsic width/height.
My widgets can be resized so the ImageView sizes are variable. Even if I set the width and height statically to some large number like this...
Bitmap bitmap = Bitmap.createBitmap(300, 300, Config.ARGB_8888);
then the resulting bitmap just has a bunch of whitespace below and to the right of a tiny image.
I guess I need to somehow draw the picture at a scaled up value as well as creating the Bitmap at size 300. Ideally I could figure out the size of the ImageView at runtime and set the proper sized Bitmap if I knew that. Is this the best approach and how would I do this? Perhaps there is a better approach I don't even know about?
I've not used android-svg but if it's using vanilla PictureDrawables, then it should be just a matter of not using the intrinsic bounds.
Try the following:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(300, 300, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
pictureDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
pictureDrawable.draw(canvas); // do not access the Picture directly, that defeats the purpose
currentBitmap = bitmap;
In short, use the Drawable, not its Picture and set the Drawable's bounds to be the full canvas.
Have you tried createScaledBitmap?
createScaledBitmap()
I have tried svg-android and it has not worked for me for this very reason. Not to mention its severely limited feature set.
The point of using vector graphics is that I can generate images of any appropriate size to fit the UI View size. Which means the generation method must accept the size requirements at run-time, and not always use width,height declared in <svg> tag.
Hence I used the native implementation: libsvg-android, which exactly does that.
It directly renders to a canvas with given size:
long objId = SvgRaster.svgAndroidCreate();
SvgRaster.svgAndroidParseBuffer(objId, readString(mInputStream, "UTF-8"));
SvgRaster.svgAndroidSetAntialiasing(objId, true);
SvgRaster.svgAndroidRenderToArea(objId, mCanvas, 0, 0, mWidth, mHeight);
I ended up modifying the underlying Artboard size to be 300. None of the scaling up methods worked. So the final code I used was that which I originally posted:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
currentBitmap = bitmap;
Once I had a 300x300 Bitmap I was able to set it into the RemoteViews using setImageViewBitmap .
This kind of defeated the purpose of using SVGs in the first place (at least as far as using them in widgets was concerned).
My problem was not the same as User117. Perhaps he did not name the outer layer 'bounds' as was required in the library. Either way, I managed to get the library working, albeit by having to modify the SVG Artboard size.
Hopefully with the increase in screen resolution Android will introduce SVGs as part of the platform soon.

create a bitmap with a bitmap inside

I know - the title may sounds strange. Let me explain:
I created an image to show you, what I'm talking about:
I got an image (Bitmap (1)), that as the size of 150w/200h.
Now I need to make the bitmap bigger ((2) 400w/400h), but the original image must have the same size. So that the image is embedded in white background.
I think one way to solve it is this:
* create a big bitmap
* create a canvas for it
* draw the original bitmap on the canvas
* draw the canvas
* generate a bitmap of the canvas
The problem for me is, that it must be done in a background thread without drawing a view.
I hope you understand me.
You can use the code bellow to achive it. Where smallBitmap is your original image and bigBitmap is the final image:
Bitmap bigBitmap = Bitmap.createBitmap(width, height , Bitmap.Config.ARGB_8888);
canvas = new Canvas(bigBitmap);
canvas.drawBitmap(smallBitmap, left, top, new Paint());
Regards.
This should do the trick.
Create a thread and in that thread object:
Create a new bitmap.
Create a canvas based on that bitmap.
Draw your bitmap to that canvas
and voila!
I hope this helps.

How to change black background color of bitmap to transparent?

I am creating Bitmap using following code:
Bitmap bm= Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
But I want to change Background color from black to transparent because I want to use this object in another Activity also. I searched a lot but I am unable to find solution.
Please help.
Thanks in advance.
Of course the Bitmap created in mode ARGB_8888 supports transparency
But the alpha channel is initially filled by 0xff, so bitmap is opaque.
You have to clear the whole bitmap including the alpha channel like this:
Canvas c = new Canvas(bm);
c.drawColor(0, Mode.CLEAR);
Bitmap images do not support transparency. You should use a GIF or a PNG.
http://www.gimpchat.com/viewtopic.php?f=8&t=1290

Categories

Resources