Java tutorial
//package com.java2s; import android.graphics.Rect; public class Main { public static Rect getFittingRectangle(float targetWidth, float targetHeight, float ratio) { //float targetRatio = targetWidth / targetHeight; float scaledWidth = targetWidth; float scaledHeight = scaledWidth / ratio; if (scaledHeight > targetHeight) { scaledHeight = targetHeight; scaledWidth = scaledHeight * ratio; } int left = (int) ((targetWidth - scaledWidth) / 2); int top = (int) ((targetHeight - scaledHeight) / 2); return new Rect(left, top, left + (int) scaledWidth, top + (int) scaledHeight); } }