How do you tint a bitmap in Android? - 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.

Related

Make the background transparent on the android canvas

How to Make transparent background on canvas android.?
I want to make a transparent background like this (see picture), does this kind of background use a bitmap?
Use a PNG format image, and set the alpha layer for transparency. What image tool are you using? Tools like GIMP & photoshop has this built in, IIRC right click the background layer (or the bottom layer if you have renamed it) and its there on the context menu.
Thats if your using a background.png in your app/main/res/drawable - or are you constructing the background purely in code?
you can, create bitmap ARGB_8888,
val bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
canvas.drawColor(Color.TRANSPARENT)

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

How to draw alpha on an image / setting pixels invisible - on fingertouch

I want to create a little scratch game. The problem is, that I can't figure out how to erase pixels from an image in android (like the eraser in gimp / photoshop).
The image is an .png with alpha channel.
AIUI, drawing operations on a canvas blend a transparent pixel with the prior value of the pixel. This is by default, and you can see it by setting a canvas to black and drawing a fully transparent shape onto it, and then drawing the underlying bitmap over an image of another canvas (result: a fully black canvas), or by setting a canvas to a partially transparent color and, drawing a shape of another partially transparent color, and then drawing this over an image (result: the original image is tinted by the first color, outside the shape; within the shape, it's tinted by both transparent colors). I don't know the blending method used by default, and looking through the docs just makes me wish I knew what book to buy so I can understand how to use what's available.
So I would set pixels to transparent by setting them 'directly', with Bitmap methods, rather than with canvas operations. Although if you need to punch a transparent shape into an overlay, you can draw the shape with a solid non-transparent color, and then manipulate the bitmap directly, mapping this color to the transparent color.
Bitmap docs. Prefer getPixels() and setPixels() to a method-call per pixel.
EDIT: ...er, did I misunderstand? You want to 'erase' pixels as in a paint program? Then just draw whatever the background color is. There's no erasure involved.

What does 'Paint' parameter do in android.graphics.Canvas.drawBitmap()?

I have a question about drawBitmap.
android.graphics.Canvas.drawBitmap(Bitmap bitmap, float left, float top, Paint paint)
What does that Paint paint? For example I have a picture.jpg and I make
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.picture);
paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawBitmap(bitmap, 0, 0, paint);
What can I do with that "paint" when I have a real picture not some canvas.drawCircle. Is there any way I can change pictures color or something like that?
Yes and another question. For example I draw circle in mspaint in 80x80 size and my background stays plain white. When I use that drawing in my program it shows circle + that white background. Is there any way that there will be displayed only circle without background. Maybe somebody can suggest some program in which I can make that happen or which code should I use in my program? (circle is just example, there can be anything)
Yes and excuse to use circle's background same as program's background is not appropriate, because my program's background isn't white or black or any other color, it is picture.
Paint objects can affect the rendering of the Bitmap. For example, they be used to mask the drawing of the Bitmap.
Save your circle as a PNG or GIF, and set the background as transparent (I do not know if MS Paint can do this).
i suggest gimp for image editing with transparency.
start a new image, delete the default layer, add a transparent layer, then paste your image over that. you can use the fuzzy select tool to trim any white space, then save as .png and you have a transparent image!

Android - Draw bitmap as one color

I have several bitmaps (game sprites) which I'd like to draw into another bitmap, however each non-transparent pixel of the source bitmap should be drawn using a single color, ignoring the original pixel color of the source. Basically, I'm trying to use the sprite as a "stamp" of a single color to be drawn into the destination bitmap.
I believe I should be using canvas.drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint), however I'm not exactly sure how I should initialize the paint object. Is this approach correct?
You don't need to perform as many steps as Romain Guy suggests, just initialize your paint with the desired color, and use Paint.setColorFilter() with PorterDuff.Mode.SRC_ATOP
myPaint.setColorFilter(new PorterDuffColorFilter(myColor, PorterDuff.Mode.SRC_ATOP));
If your destination bitmap is transparent, draw all your sprites inside that bitmap normally (you can use a null Paint.) Then, draw a filled rectangle that covers the entire bitmap, using the Porter-Duff xfermode called SrcIn (Source In.)

Categories

Resources