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.
Related
The goal is to draw some hexagons on the screen and set different background images for those hexagons (I'm making a broad game). I have read about the Shape in XML file, but i want to draw it programmatically. I have drawn a shape with the help of canvas like this:
Path hexPath = hex.getPath();
canvas.clipPath(hexPath);
canvas.drawColor(Color.RED);
how can i set up the background image for this shape ?
I also have read about cropping a Bitmap, but is not practical for me.
Thanks.
I have a stack of bitmaps that I need to render one above the other. I achieve this with a relative layout and several ImageViews on top of each other which all have a Bitmap assigned to it.
This works great, but when the top layers is semi-transparent, the colours of the lower bitmap are off.
All my bitmaps use Config.ARGB_8888.
Say the top layer is red with an alpha of 50% and the bottom layer is green with an alpha of 100%.
I can either set the colour of the bitmap to red, then the alpha of the ImageView to 0.5f and it will render the green colour below fine (darker green with some red mixed in).
If I set the bitmap pixels to a 50% red like this: bmp.eraseColor(0x7Fff0000); and leave the imageView alpha on 100%, the green below will be displayed as yellow, mixing red and green, rather than overlaying it.
Unfortunately I can not use the (working) fist version because the alpha on the Bitmap above is not going to be uniform.
Is there a blend mode setting to use true colours when using semi transparent pixels in a Bitmap?
EDIT: I have also tried to set several PorterDuffXfermodes to the ImageViews but none gives the right result.
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY)); //OVERLAY//ADD//SCREEN//DARKEN//LIGHTEN
imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, paint);
Got it, needed to premultiply the alpha to get the desired result.
I am drawing some buttons on the screen. Each with a different color, but the same shape, designed in Photoshop with all sort of reflections and shines.
I want to use a single bitmap of the shape and change its color programmatically, preserving all the reflections and shadows. This is what I do right now:
Get the shape into a ARRGB_8888 bitmap (even though all colors are shades of gray)
Copy the bitmap pixels to 3 buffers: Image, Highlights, Shadows
The reference grayshade is RGB[128,128,128]
In the Highlights buffer, zero all pixels below the reference (+ threshold)
In the Shadows buffer, zero all pixels above the reference (- threshold)
From the Highlights and Shadow buffers create Highlights and Shadow Bitmaps
Draw the original grayscale image using a PorterDuffColorFilter on mode MULTIPLY
Draw on top the shadows, using the Shadows bitmap and XferMode DARKEN
Draw on top the highlights, using the Highlights bitmap and XferMode LIGHTEN
I do get a result, but I realize that the final button color is not the target color, but a darker shade of it, because the MULTIPLY mode with a reference of 128 cuts all components in half.
I tried to set the reference to a whiter shade of gray, but then the highlights get saturated.
I tried to use SRC_IN on step 7 above, and I get the target color only on areas that are neither highlights nor shadows.
See the results:
Not sure what I need to ask, but I want to get the buttons with the exact target color and its highlights and shadows. Maybe I am generating the Highlights and Shadows masks wrong, or maybe I am using the wrong blending modes. Or maybe it is something else.
I have an 8x8 Image. (bitmap - can be changed)
What I want to do is be able to draw a shape, given a Path and Paint object onto my SurfaceView.
At the moment all I can do is fill the shape with solid colour. How can I draw it with a pattern.
In the image you can see the brush pattern (The cross). It can be anything from a cross to a donut or an elf.
How would I go about drawing this pattern background.
I also eventually want to apply colours to it.
So far, my theory is to create a clip area of the shape, and tile the bitmaps until area is covered, but this is extreme overkill in processing. Nor sound ideal.
In terms of colouring, I can edit the brushes to be alpha, fill the same with background colour, then draw the images on top. The real issue it the tiling of such patterns.
Ive found a few questions of a similar nature, all unanswered, and/or not applicable to my situation. (use of xmls on views etc)
Did you checked this blog. Its using BitmapShader
Example:
//Initialize the bitmap object by loading an image from the resources folder
fillBMP = BitmapFactory.decodeResource(m_context.getResources(), R.drawable.cross);
//Initialize the BitmapShader with the Bitmap object and set the texture tile mode
fillBMPshader = new BitmapShader(fillBMP, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
fillPaint.setStyle(Paint.Style.FILL);
//Assign the 'fillBMPshader' to this paint
fillPaint.setShader(fillBMPshader);
//Draw the fill of any shape you want, using the paint object.
canvas.drawCircle(posX, posY, 100, fillPaint);
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!