I have no problem doing:
currentImageButton.setImageBitmap(bitmap);
However, I have yet to be able to get the image bitmap back. I am trying to get the image bitmap BACK from the ImageButton and use it to set the background of my canvas.
currentImageButton.buildDrawingCache();
Drawable myDrawable = currentImageButton.getDrawable();
Bitmap anImage = ((BitmapDrawable) myDrawable).getBitmap();
canvas.drawBitmap(getResizedBitmap(anImage, 1000, 1000));
None of this seems to work though. I've even tried using myDrawable to set change the background of my view, just incase it was something wrong with canvas but that doesn't work either.
Bitmap bitmap = ((BitmapDrawable)imageBitmapInstance.getDrawable()).getBitmap();
Related
I am applying a color filter to an imageview and trying to get the bitmap from that imageview.
ColorFilter filter = new LightingColorFilter(0x000000, myColor);
image_view.setColorFilter(filter);
Bitmap bitmap = ((BitmapDrawable)image_view.getDrawable()).getBitmap();
but the bitmap is not keeping the color filter I applied to it and I am getting the original image bitmap only.
below are the duplicate posts asked but it was not helpful:
Drawable loses color filter after converting into bitmap
Convert filtered drawable to bitmap
I figured it out finally!
We need to do following thing to get the bitmap with color filter applied on it.
image_view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(image_view.getDrawingCache());
Hope this will help someone struggling like me.
I have a canvas and an ImageButton. I am trying to get the background image of my ImageButton and set it as the background image of my canvas. My canvas uses the drawBitmap() function to set it's background, so it takes in a bitmap. However, the getBackground() function for my ImageButton returns a drawable.
How do I get the background of my ImageButton and set it to the background of my canvas?
canvas.setBackground(drawableFromImageButton.getConstantState().newDrawable());
when canvas is view, setBackground method is applicable.
You can get the Bitmap from the Drawable as following way
Drawable drawable = imageButton.getBackground();
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
try like this. it might be helpful.
Drawable drawable = imageButton.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
after that call
canvas.setBitmap(bitmap);
If you making a custom Canvas you can directly set drawable to set the background.
Follow this-
customCanvas.setBackground(imageview.getBackground());
The documentation says like tbis-
void setBackground(Drawable background)
Set the background to a given Drawable, or remove the background.
The draw is used only to draw on tope of the canvas.
I hope this would help you.
So I want to set a Bitmap created from drawable into a SeekBar's progress. And I make it so:
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Drawable drawable = getResources().getDrawable(R.drawable.seekbar_bg_full);
Canvas canvas = new Canvas(bmp);
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas); // I assume here drawable must be drawn but its not
// canvas.drawBitmap(bmp, 0 , 0, null); // does nothing as 4 me
// encode/decode to detach bitmap from 9patch
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 0, baos);
final byte[] bytes = baos.toByteArray();
bmp.recycle();
bmp = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
// ClipDrawable is intented to be used as progressDrawable in SeekBar
ClipDrawable progressDrawable = new ClipDrawable(new BitmapDrawable(getResources(),bmp), Gravity.LEFT, ClipDrawable.HORIZONTAL);
// if not set this drawable to an ImageView then no progress will be shown by SeekBar at all
//ImageView imgFake = (ImageView) findViewById(R.id.fakeImageView);
//imgFake.setImageDrawable(progressDrawable);
mySeekBar.setProgressDrawable(progressDrawable);
width and height are valid values here (like 460 and 30). As you can see there are 2 code lines about ImageView are commented. This ImageView persists on a layout and its visibility is INVISIBLE. If I comment those 2 lines like shown then there will be no visible progress, like drawable is empty or transparent. Looks like this ImageView makes drawable to really draw itself. But I don't like to use a fake ImageView just to make the "magic" happen so the question is - how to make it work without this fake ImageView.
Please don't suggest me ways how to properly set SeekBar progress like:
ClipDrawable progressDrawable = new ClipDrawable(getResources().getDrawable(R.drawable.seekbar_bg_full), Gravity.LEFT, ClipDrawable.HORIZONTAL);
mySeekBar.setProgressDrawable(progressDrawable);
or xml selectors ways or any altrnative ways since I know about it already and my quiestion is not really about it. I just need to make it work my way.
I just need to make my bitmap or canvas or whatever really drawn.
A bit more details if you want (optional to read). The problem is about the drawable seekbar_bg_full - its a 9-patch png. And all need is to obtain a not NinePatchDrawable-linked resulting Bitmap. Assume I have a 460x30px view with 9patch image set as src or background and the 9patch image is stretched just like it should to. So I need to get a Bitmap this view contains and this Bitmap should not be linked somehow to a 9patch. Thats why I encode bitmap to an byte-array and then decode it back - its just to get rid of 9patch. If there is a more esay way to get a resulting bitmap from 9patch (some magic around NinePatchDrawable) - I would like to know about it.
Ok, I figured out how to get rid of fake ImageView and make drawable to draw itself: all I have to do is to call setBounds() method on a drawable:
ClipDrawable progressDrawable = new ClipDrawable(new BitmapDrawable(getResources(),bmp), Gravity.LEFT, ClipDrawable.HORIZONTAL);
progressDrawable.setBounds(0, 0, width, height);
mySeekBar.setProgressDrawable(progressDrawable);
Now I don't have to use ImageView, at last!
However my code is a really long story to get rid of 9patch features in drawable.
I'm trying to capture an imageView and out it in a bmp. It currently works but, i am not able to make the captured imageView the correct size. This gives me a problem was black bar on the side of my imageView when it put the drawingcache as a bmp back into the imageview for display.
My code is like this:
imageView.setDrawingCacheEnabled(true);
Bitmap bm = Bitmap.createBitmap(imageView.getDrawingCache());
I tired doing :
Bitmap bm = Bitmap.createBitmap((int) dw, (int) dh,imageView.getDrawingCache());
but imageView.getDrawCache (BMP type) is not same type as Bitmap.Config.ARGB_8888. I'm not sure how to tackle this specific problem. Any help would be nice.
Please read this question for more detail and screenshots.
Black bars on the side of screen capture on imageView
I have a board game app that creates a lot (hundreds) of bitmaps, and constantly changes them as the game progresses. These bitmaps are created, not loaded from a resource... so they have no R.id to refer to.
I would like to animate some of them, e.g. a bitmap moving from one loction to another when a player taps to move it. What is the best way to do this?
Note, this is 2.1 and the bitmaps are drawn on a canvas via a matrix translate.
Make this function to get image from drawable folder
private Bitmap getImageBitmap(){
int resID = getResources().getIdentifier(uri, "drawable",
"a.b.c");
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), resID);
return mBitmap;
}
For set the image , where do you you want>
ImageView imageview01.setImageBitmap(getImageBitmap("name of bitmap");
if you want more about bitmap. then you may try this links.
http://developer.android.com/reference/android/widget/ImageView.html
drawable getResources().getIdentifier problem
setImageURI / setimagebitmap to fetch image from net and display in image view