I am trying to use Android's PorterDuff to draw a shadow that will only appear where there is something below it (not where the background image is transparent) and I am having great difficulty getting it to work. I have written the following code to test the various PorterDuff.Modes and they do not seem to be working as described here:
https://developer.android.com/reference/android/graphics/PorterDuff.Mode.html
From that page, I would say the effect I'm looking for is SRC_ATOP.
Here is the code I am using, and below it an image of the result:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.v4.content.ContextCompat;
import android.view.View;
public class PorterDuffTest extends View {
private int mTexWidth;
private int mTexHeight;
private Bitmap dstBmp;
PorterDuffTest(Context c) {
super(c);
dstBmp = BitmapFactory.decodeResource(c.getResources(), R.drawable.src);
mTexWidth = dstBmp.getWidth();
mTexHeight = dstBmp.getHeight();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//canvas.drawRGB(255, 255, 255);
canvas.save();
canvas.translate(40.0f, 40.0f);
//COLUMN 1
drawBlendedBitmap(canvas, PorterDuff.Mode.DARKEN);
drawBlendedBitmap(canvas, PorterDuff.Mode.LIGHTEN);
drawBlendedBitmap(canvas, PorterDuff.Mode.OVERLAY);
drawBlendedBitmap(canvas, PorterDuff.Mode.ADD);
canvas.restore();
canvas.save();
canvas.translate(40.0f + mTexWidth + 40.0f, 40.0f);
//COLUMN 2
drawBlendedBitmap(canvas, PorterDuff.Mode.CLEAR);
drawBlendedBitmap(canvas, PorterDuff.Mode.MULTIPLY);
drawBlendedBitmap(canvas, PorterDuff.Mode.SCREEN);
drawBlendedBitmap(canvas, PorterDuff.Mode.XOR);
drawBlendedBitmap(canvas, PorterDuff.Mode.OVERLAY);
canvas.restore();
canvas.save();
canvas.translate(40.0f + mTexWidth + 40.0f + mTexWidth + 40.0f, 40.0f);
//COLUMN 3
drawBlendedBitmap(canvas, PorterDuff.Mode.SRC);
drawBlendedBitmap(canvas, PorterDuff.Mode.SRC_OUT);
drawBlendedBitmap(canvas, PorterDuff.Mode.SRC_IN);
drawBlendedBitmap(canvas, PorterDuff.Mode.SRC_ATOP);
drawBlendedBitmap(canvas, PorterDuff.Mode.SRC_OVER);
canvas.restore();
canvas.save();
canvas.translate(40.0f + mTexWidth + 40.0f + mTexWidth + 40.0f + mTexWidth + 40.0f, 40.0f);
//COLUMN 4
drawBlendedBitmap(canvas, PorterDuff.Mode.DST);
drawBlendedBitmap(canvas, PorterDuff.Mode.DST_IN);
drawBlendedBitmap(canvas, PorterDuff.Mode.DST_OUT);
drawBlendedBitmap(canvas, PorterDuff.Mode.DST_ATOP);
drawBlendedBitmap(canvas, PorterDuff.Mode.DST_OVER);
canvas.restore();
}
private void drawBlendedBitmap(Canvas canvas, PorterDuff.Mode mode) {
Paint paint = new Paint();
canvas.drawBitmap(dstBmp, 0.0f, 0.0f, paint);
paint.setXfermode(new PorterDuffXfermode(mode));
paint.setColor(ContextCompat.getColor(getContext(), R.color.tileBlue));
canvas.drawRect(15, 15, 135, 135, paint);
canvas.translate(0.0f, 40.0f + mTexHeight);
}
}
I have been playing with this for a number of hours now and I'm really struggling to make any progress, so any help would be greatly appreciated. Thank you in advance!