I have two images.
An image with a red rectangle, and image all white. I would like to paint with your finger on the white image only where the other image is the red rectangle.
The image with the red rectangle should not be visible.
How can I do?
Create bounds for each image, e.g. with a Rect set to the cords of each image (position & size). In the view where your overriding onDraw() in, set the onTouchListener to the view itself.
In onTouch() check the event.getX()/getY() is within the bounds of the white image. Then use whiteImage.setPixel() to set the individual pixels of the Bitmap image. Alternatively use Canvas.drawPoint() instead of manipulating white bitmap image itself.
In regards to not displaying the red rectangle... just don't draw it?
Edit:
To your comment about non square/rect shapes. I would still check for the touch event in the rect and then pass it to the image if it has hit the shape.
Within the shape (I'm assuming it is a bitmap) you would do Bitmap.getPixel(x, y) and see if it is == to Color.White, if it is.. change it to whatever colour!
Related
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 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.
I have a bitmap out of which I'm cutting out a multipi point polygon. I'm curious what the correct process is for taking the pixels within the arbitrary shape and copying them onto a new bitmap where the rest of the pixels are transparent. The objective is to allow the user to trace the shape and then remove everything outside the polygon.
I have the polygon part worked out (as a an array of points), but now am stumped as to how to transfer just the selected pixels to a new Bitmap.
TIA
Not sure how your code works, but here's an idea on how to do it:
Calculate the bounding rectangle of the selected area (find min x, min y, max x and max y from your points).
Crop your image to the bounding rectangle using any of the Bitmap or Canvas-methods.
Create a Path from your points, all moved into your new bitmap (x-=minX, y-=minY);
Set your Paths FillType to one that is inverse (fill the outside).
On your new cropped canvas, draw the Path using a paint with the Xfermode as PorterDuff.CLEAR, which removes all color.
I am trying to subclass ImageView and draw something on Bitmap. However I cannot find a way to get the Rect in which Bitmap is drawn. I can only get the Rect in which ImageView is drawn by getDrawingRect(Rect) method of ImageView. Below is an illustration of what I want to get:
The Rect I want is the blue one. Thanks in advance.
The given image will be drawn in the ImageView based on the attributes given e.g., height, width, scaling factors, etc.
So that the getDrawingRect() method giving the entire area of ImageView. If you change the drawable inside ImageView the blue colored area may change based on the image properties and imageview properties. But the yellow colored area won't change because it is fixed and based on the ImageView only, its independent of image displayed.
I think no chance to get the Rect of bitmap drawn. Don't think my answer is 100% correct, it is just a suggestion only.
You may get information about blue colored area from Drawing Cache. Try it once.
I hope it may help you.
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!