Java tutorial
//package com.java2s; import android.graphics.Rect; public class Main { public static Rect getFramingRect(int imgWidth, int imgHeight) { int width; int height; Rect framingRect; if (imgWidth > imgHeight) { if (imgWidth * 9 / 16 > imgHeight) { width = imgHeight * 16 / 9; height = imgHeight; } else { height = imgWidth * 9 / 16; width = imgWidth; } } else { if (imgWidth * 16 / 9 > imgHeight) { width = imgHeight * 9 / 16; height = imgHeight; } else { width = imgWidth; height = imgWidth * 16 / 9; } } int leftOffset = (imgWidth - width) / 2; int topOffset = (imgHeight - height) / 2; System.out.println("leftOffset=" + leftOffset + ":topOffset=" + topOffset); framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height); return framingRect; } }