Get the background of an ImageButton as a bitmap - android

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.

Related

How to set a hex color to an ImageView inside a notification?

I'm trying to set a color as an image to an ImageView that exists inside a custom notification. I can give it predefined colors by writing this line:
remoteViews.setImageViewResource(R.id.imageView, R.color.Green);
But i want to be able to give it hex color values, and set that as the color. I tried setting the color as the background of the ImageView, but i couldn't get access to it, and the only options i have to access the ImageView are these:
remoteViews
.setImageViewResource()
.setImageViewBitmap()
.setImageViewIcon()
.setImageViewUri()
Is there a way to pass in a color by parsing the hex value to any of these?
I think if I was able to create a bitmap and set the bitmap to the image view, it could be done, so i wrote the code bellow to create the bitmap:
int[] colors = {Color.parseColor("#64DD17"), Color.parseColor("#D32F2F")};
Bitmap bitmap = Bitmap.createBitmap(colors, 10, 10, Bitmap.Config.RGB_565);
, but i get a java.lang.ArrayIndexOutOfBoundsException. I tried with one element in the colors array too, but same error.
What is my error here?
The int[] is your image; so you create an image 2px wide, since that's the length of your array. However, you call createBitmap and specify that the image is in fact 10pxx10px; this is why the ArrayIndexOutOfBoundsException is thrown, as Android starts looking for pixels that aren't there.
Using Bitmap bitmap = Bitmap.createBitmap(colors, 2, 1, Bitmap.Config.RGB_565); would solve this problem, albeit generate a very tiny image!
Simple solution: generate png images(or use svg) to put inside the image view, instead of the color you want.
but, if u want to use that approach, you can create a colored bitmap using something like that:
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
image.eraseColor(android.graphics.Color.GREEN);

How to get the image bitmap of an ImageButton?

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();

How to set canvas as background for layout

In my application i have designed a canvas. I want to set the canvas as background Resource for my layout.Can any one help me how to do.
Thanks in advance
Yon need a Bitmap for this purpose. And the you can set the Bitmap as background for your layout.
public Canvas (Bitmap bitmap)
From the doc:
Construct a canvas with the specified bitmap to draw into

Android: BitmapDrawable.Draw(Canvas) Doesn't seem to work

I am trying to tile a 20x20 background onto my Custom View but for some reason I am unable too.
BitmapDrawable background;
background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.back));
background.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
background.draw(canvas);
Does anyone have an idea why it isn't working?
You can use BitmapDrawable, but you have to set the bounds first so it knows how much tiling to do:
BitmapDrawable background;
background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(),R.drawable.back));
//in this case, you want to tile the entire view
background.setBounds(0, 0, myView.getWidth(), myView.getHeight());
background.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
background.draw(canvas);
You are probably getting warnings in your log regarding SKImageDecoder failing. If you are creating the resource via xml you need to retreive it via (BitmapDrawable) getResources().getdrawable(id)
You have it backwards. Instead of passing your View's canvas to the bitmap's draw method, draw your Bitmap to the View's canvas using Canvas.drawBitmap

How do you tint a bitmap in Android?

I draw a bitmap onto a canvas using the following call:
_playerImage = BitmapFactory.decodeResource(getResources(), R.drawable.player);
How can I now tint this image white?
I'm trying to make the image flash white like in top-scrollers when an enemy is hit by a bullet.
Do I need to use something other than BitmapFactory?
You can use a ColorFilter on your Paint when you draw the bitmap.

Categories

Resources