Java tutorial
//package com.java2s; import android.graphics.Point; public class Main { public static Point restrictSizeInSize(Point size, Point inSize) { if (size.x * inSize.y > inSize.x * size.y) { int width = inSize.x; int height = (int) (size.y * 1.0 / size.x * inSize.x); return new Point(width, height); } else if (size.y * inSize.x > inSize.y * size.x) { int height = inSize.y; int width = (int) (size.x * 1.0 / size.y * inSize.y); return new Point(width, height); } else { return inSize; } } }