Java tutorial
//package com.java2s; //License from project: Open Source License import android.graphics.Bitmap; import android.graphics.Rect; public class Main { public static Bitmap cropCenterInside(Bitmap src, Rect rect) { int width = Math.min(src.getWidth(), rect.width()); int height = Math.min(src.getHeight(), rect.height()); int[] rowData = new int[width]; int stride = src.getWidth(); int x = (src.getWidth() - width) / 2; int y = (src.getHeight() - height) / 2; Bitmap dest = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); int delta = 0; while (delta < height) { src.getPixels(rowData, 0, stride, x, y + delta, width, 1); dest.setPixels(rowData, 0, stride, 0, delta, width, 1); delta++; } return dest; } }