I'm having trouble cloning a BitmapDrawable. I tried the answer in this post but it creates a "shallow" copy, and I need a "deep" copy so I can alter the pixels in the clone without affecting the original.
I also tried this:
Bitmap bitmap = bdOriginal.getBitmap();
BitmapDrawable bdClone = new BitmapDrawable(getResources(), bitmap.copy(bitmap.getConfig(), true));
But it seems to create an immutable clone even though I set the mutable parameter in Bitmap.copy() to "true". That is, color filters don't appear to change the clone. Am I doing it wrong? (EDIT: I used the debugger to confirm bitmap.mIsMutable = true)
To summarize, I need a clone of a BitmapDrawable that can be altered with color filters without affecting the original. Any suggestions?
Thanks in advance...
Create new Bitmap of the same size.
Create canvas for this new Bitmap
Draw your first Bitmap into this canvas.
Example:
Bitmap copy = Bitmap.createBitmap(original.getWidth(), original.getHeight(), original.getConfig());
Canvas copiedCanvas = new Canvas(copy);
copiedCanvas.drawBitmap(original, 0f, 0f, null);
Related
I tried searching for a relevant example but found none, so I'll try to be as precise as I can.
Back in the gingerbread era I made a draw bitmap to canvas code that worked flawlessly and here it is.
//this one is drawing the background for my picture
mBackground = BitmapFactory.decodeResource(getResources(), R.drawable.base);
//setting a new canvas
mComboImage = new Canvas(mBackground);
//adding another bitmap to the canvas, don't worry about the size or the i variables
mBackImage = BitmapFactory.decodeResource(getResources(), R.drawable.base);
mBackImage=Bitmap.createScaledBitmap(mBackImage, size, 105, false);
mComboImage.drawBitmap(mBackImage, 100f+1f*size*i, 170f, null);
mBackImage.recycle();
//this is setting my background for an icon to the completed image
Drawable d =new BitmapDrawable(getResources(),mBackground);
aaa.setBackground(d);
anyways the code doesn't seem to fit now. One problem I have faced is converting the bitmap into mutable which you can check out here if you are stuck on it as I was for a while.
My problem is that the background is perfectly drawn but the mBackImage doesn't show up at all.
What worries me more is that this used to work perfectly before.
I really have tried searching for a newer explanation but haven't really found any on stackoverflow so
I drew bitmap as background image for my app as well. I used: setBackgroundDrawable() instead of setBackground() in your last statement.
Also, when preparing the bitmap, I set two options:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable=true;
bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.image1), options);
You can give it a try.
to learn the functionality of missile android game app, i thought of doing some changes to it, so started up the editing of background change, i had googled and placed the code in canvas,
Bitmap background = Bitmap.createBitmap( 0, 0, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(background);
c.drawBitmap(background, 10, 010, null);
i had tried many things but nothing worked out, and also here in the above code not given the path for drawable, where should i give the path for image?
any helps plz..
Hope this code will give you some clue about this...
//Here you can decode it from any source like SD card as i shown,Resources or from assets
Bitmap mBitmapFromSdcard = BitmapFactory.decodeFile("/mnt/sdcard/download/tux.png");
Canvas mCanvas = canvas;
if (mBitmapFromSdcard != null) {
mCanvas.drawBitmap(mBitmapFromSdcard, 0, 0, null);
}
I have a loaded Bitmap which I would like to convert to set the config to Bitmap.Config.RGB_565. Is there a simple way of converting a Bitmap to this configuration after the Bitmap is already loaded into memory? For example, below I have a bitmap being decoded from the application resources, however, how would I convert an already loaded Bitmap to RGB_565? I'm sure it's something simple, however, I'm fairly new to working with Bitmaps and after a few hours of looking online, unfortunately I couldn't find what I needed specifically.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig=Bitmap.Config.RGB_565
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.myphoto ,options);
You can also try this:
Bitmap converted = original.copy(Config.RGB_565, false);
From the documentation of Bitmap.copy():
Tries to make a new bitmap based on the dimensions of this bitmap, setting the new bitmap's config to the one specified, and then copying this bitmap's pixels into the new bitmap. If the conversion is not supported, or the allocator fails, then this returns NULL.
Looking through the native source code, you should be fine converting between any values of Bitmap.Config.
I haven't tested this but it should work:
private Bitmap convert(Bitmap bitmap, Bitmap.Config config) {
Bitmap convertedBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), config);
Canvas canvas = new Canvas(convertedBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawBitmap(bitmap, 0, 0, paint);
return convertedBitmap;
}
call the methods like this:
Bitmap convertedBitmap = convert(bitmap, Bitmap.Config.RGB_565);
You can do all kinds of additional transformations like rotating, stretching etc. if you use the drawBitmap with a Matrix.
Found the answer here https://stackoverflow.com/a/12148450/1364673, thanks to
siliconeagle.
The solution is to create a new bitmap with the required encoding as per link above example.
In activity-context it's easy to override onDraw()-method by creating a custom class extended to View or in this case ImageView. Accessing that view is also easy with LayoutInflater or it's instance.
Now I'm developing an AppWidget, but there were used RemoteViews instead.
Any ideas how do that?
Ok. It seems that I've found a solution here in stackoverflow:
"Just create a Bitmap, create a Canvas with it, and then draw on it
using what was your onDraw code. Then create an ImageView (which is
allowed in a widget) and set the image it displays to your Bitmap."
(Dynamically adding a View to an android widget)
Create a canvas through bitmap:
(creating an empty bitmap and drawing though canvas in android)
Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap bmp = Bitmap.createBitmap( 150, 150, conf ); // this creates a MUTABLE bitmap
Canvas cv = new Canvas( bmp );
//cv.draw...
After drawing I just need to push that bitmap to remoteViews.
Hope it helps you too!
=)
I was trying to set a bitmap image to a canvas using setBitMap ,at that time I got an IllegalStateException.This canvas have some images on it currently, I am trying to replace it.
Any one have any idea why this happened?
Code Snippet
editBm = Bitmap.createBitmap(951, 552, Bitmap.Config.ARGB_8888);
Canvas mCanvas=new Canvas(editBm);
eBit=LoadBMPsdcard(filePath); ---->returns a bitmap when the file path to the file is provided
Log.i("BM size", editBm.getWidth()+"");
mCanvas.setBitmap(eBit);
I am not getting any NullPointer errors and the method LoadBMPsdcard() is working good.
Please let me know about any ideas you have ...
Thanks in advance
Happy Coding
IllegalStateException could be thrown because you're loading a Bitmap (eBit) and use mCanvas.setBitmap(eBit) without checking if the bitmap is mutable. This is requiered to draw on the Bitmap. To make sure your Bitmap is mutable use:
eBit=LoadBMPsdcard(filePath);
Bitmap bitmap = eBit.copy(Bitmap.Config.ARGB_8888, true);
canvas.setBitmap(bitmap);
Try to use drawBitmap instead of the setBitmap. It looks like you've already set a bitmap to draw into by passing it to the canvas constructor, so now you just need to draw everything onto it.
Canvas.setBitmap() throws IllegalStateException if and only if Bitmap.isMutable() returns true. Bitmap.createBitmap() builds an immutable Bitmap instance only, in all of its forms. To create a mutable bitmap you either use new Bitmap(), or Bitmap.copy(true), depending on whether you have a source bitmap that you want to start with. A typical block for me looks like:
Bitmap image = ...
Canvas c = new Canvas(image.isMutable()?image:image.copy(true));
...
This assumes, of course, that you don't mind clobbering the source Bitmap (which I generally don't but that's by no means universal).