Selectively masking in android canvas - android

I have a canvas on which the background image if first drawn.
Then another image is drawn on top of the background.
I have a Gradient object that moves across the screen. The Paint used in the Gradient has its Xfermode set as,
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
The effect works but it is also applied on the background image. How can i avoid the background image from affected by the mask on top?

The paint will be applied on the whole of your Canvas. As your background is drawn using the same Canvas than the Gradient object, of course using a custom Xfermode will affect said background!
One possible solution would be to separate your background and your foreground in 2 different Canvas objects backed up by separate Bitmap objects, then merge the layers together as one would do in Photoshop. I posted a sample code that does just this on StackOverflow a while ago, here is the link to it:
https://stackoverflow.com/a/10370828/1350375

Related

How to implement a real eraser in my drawing app?

I am creating a simple drawing app using kotlin. I want to implement an eraser and was not able to find any proper solution for this. Every solution either uses white color as a substitute for eraser or completely resets the canvas after clicking on eraser button. As I have also implemented the feature of importing background images from the gallery, I can't use white color as eraser.
There is no magic eraser in canvas, because erasing is nothing but drawing something onto the canvas. You can't set the paint color to Transparent and draw on canvas to erase what is already drawn. This simply draws something that is transparent, which is basically nothing.
So you will need to implement an eraser by using a Color. Normally this is white, because the background is usually set to white. So this gives the appearance of erasing the content.
In your case, you have a background image. To implement an eraser for this, you will need 2 canvas. The background image will be drawn in the 1st canvas. All the other content (drawCircle, drawRect, drawPath, etc...) are drawn onto the 2nd canvas. Now, you could take the bitmap out of the 2nd canvas and draw that in the 1st canvas (drawBitmap). This gives the final appearance.
Now, in this setup, you could use "White" as a color to erase from the 2nd canvas. While drawing 2nd canvas content onto the 1st, use one of the PorterDuff mode, so that the "White" goes away.

Android Fill rectangle with multiple colors

Can a rectangle be filled with multiple colors.I mean using canvas drawrect method, a rectangle is drawn, and it is to be filled with 3(Example) colors. It can be done by drawing 3 different rectangles, but looking for some optimized solution.

draw multiple circles and set them as ImageView background in android

I have an image view with a (not really) complex background. The background is to be the composition of three circles, each with different dynamically set colors.
one circle, the largest, is actually a perimeter (i.e. stroke)
one circle, the second largest, is concentric with the perimetric circle
one circle, the smallest, sits at the base of the other two circles (so that it is not concentric).
I successfully create the 3-circle background using layer-list. But the problem is that I am not able to change the colors in the layer-list dynamically. Changing the color of these circles is a crucial part of the design.
So since my ImageView is part of a custom view anyway, I am now thinking of using the canvas in onDraw(Canvas canvas) to create my three circles and set them as the background of the ImageView. However, the problem with this approach is that I don't know how to set my composite image (the three circles) as the background of my ImageView.
Any snippet of code solving this problem is greatly appreciated.

On an android Canvas, how do I draw overlapping shapes with non-interacting alphas?

On an android Canvas, if I draw a circle with alpha 0xCC and color Color.RED and then draw another circle which partially overlaps the first circle with the same parameters, I'll end up with a venn diagram.
Here is a random example I found (just ignore the [Text] in there). I want to draw overlapping circles like in this diagram, but I don't want the center to be darker, but I do want the whole thing to have alpha so that the map underneath is visible.
Is there a way to do this directly or do I need to draw to a bitmap without alpha and then set the alpha for the whole bitmap and draw it to a canvas? (I haven't used bitmaps yet, so I am not sure how they are used yet.)
The easy way would be your suggested solution, ie. drawing all circles with no alpha to a bitmap, then draw that bitmap to another one using the desired alpha.
The hard way would be using blend modes, specifically PorterDuff.Mode in Android. An example can be found here.

Android draw with blur

I need do draw on Android's Canvas using Blur effect,
it is a very simple feature,
I need to draw a circular area, which is blurred (the foreground) and the background transparent, I can do everything with manipulating the colour alpha to do it with custom transparency but I need it to be blurred instead of transparent..
any ideas?
(For those who are coming back, though this is an old question)
On the Paint object which you are using to draw the Color, set
paint.setMaskFilter(new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL))
Then, in Android Manifest.xml, set
android:hardwareAccelerated="false"
for your activity
(Blur doesn't work when hardware acceleration is true)
For more explanation refer
Android BlurMaskFilter has no effect in canvas.drawOval while text is blurred
If you're not gonna need to manipulate it afterwards you could just draw to a buffer and blur it yourself pixel by pixel, then blit that buffer to screen when you need to paint it.
Or this: http://developer.android.com/reference/android/graphics/BlurMaskFilter.Blur.html
Take a look at this: Android, Blur Bitmap instantly?
Again I would recommend you first to draw into a bitmap. Scale it down (try different sizes), and the scale it back up to the original size (either as a new bitmap or if you use hw acceleration do it while you draw).

Categories

Resources