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