Java tutorial
//package com.java2s; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Xfermode; import android.graphics.Bitmap.Config; public class Main { public static Bitmap transferMode(Bitmap src, Bitmap mask, Xfermode xfermode) { final int width = mask.getWidth(); final int height = mask.getHeight(); final Bitmap dst = Bitmap.createBitmap(width, height, Config.ARGB_8888); final Canvas canvas = new Canvas(dst); final Paint paint = new Paint(); paint.setAntiAlias(true); final int srcWidth = src.getWidth(); final int srcHeight = src.getHeight(); canvas.save(); // Scale down the image first if required. if ((width < srcWidth) || (height < srcHeight)) { float radioX = (float) width / srcWidth; float radioY = (float) height / srcHeight; float radio = 1f; float dx = 0f; float dy = 0f; if (radioX > radioY) { radio = radioX; dy = (height / radioX - srcHeight) / 2.0f; } else { radio = radioY; dx = (width / radioX - srcWidth) / 2.0f; } canvas.scale(radio, radio); canvas.translate(dx, dy); } canvas.drawBitmap(src, 0, 0, paint); canvas.restore(); if (xfermode != null) { paint.setXfermode(xfermode); canvas.drawBitmap(mask, 0, 0, paint); } return dst; } }