Java tutorial
//package com.java2s; //License from project: Open Source License import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; public class Main { /** * cut a circle from a bitmap * * @param picturePath complete path name for the file. * @param destCube the cube dimension of dest bitmap * @return the bitmap */ public static Bitmap cutCircleFromBitmap(String picturePath, int destCube) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inDither = false; //Disable Dithering mode opts.inPurgeable = true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared opts.inInputShareable = true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future opts.inTempStorage = new byte[32 * 1024]; Bitmap bitmapImg = BitmapFactory.decodeFile(picturePath, opts); int cube = destCube; if (bitmapImg == null) return null; int smallest = Math.min(bitmapImg.getWidth(), bitmapImg.getHeight()); Bitmap output = Bitmap.createBitmap(cube, cube, Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); int left = (int) ((bitmapImg.getWidth() - smallest) * 0.5); int top = (int) ((bitmapImg.getHeight() - smallest) * 0.5); final Rect rectSrc = new Rect(left, top, left + smallest, top + smallest); final Rect rectDest = new Rect(0, 0, cube, cube); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawCircle(cube / 2, cube / 2, cube / 2, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmapImg, rectSrc, rectDest, paint); bitmapImg.recycle(); return output; } }