/**
* 计算长宽比
*
* 先计算原始长宽比(目标宽高/原始宽高),在乘上原始宽高,即:
* 求高 -> 原始高 × ( 目标宽 ÷ 原始宽 )
* 求宽 -> 原始宽 × ( 目标高 ÷ 原始高 )
*
* @param fromWidth 原始宽度
* @param fromHeight 原始高度
* @param toWidthOrHeight 目标宽度或高度
* @param isWidth <code>toWidthOrHeight</code>是宽度还是高度
* @return <code>isWidth</code>为true则返回结果为高度,否则为宽度
*/
public static int calcAspectRatio(int fromWidth, int fromHeight,
int toWidthOrHeight, boolean isWidth) {
if( isWidth ) {
return (int) ((double) fromHeight * ( (double) toWidthOrHeight / (double) fromWidth ));
}else {
return (int) ((double) fromWidth * ( (double) toWidthOrHeight / (double) fromHeight ));
}
}
public static void main(String[] a) {
System.out.println( "1920*" + clacAspectRatio( 1440, 2880, 1920, false ) );
System.out.println( "1080*" + clacAspectRatio( 1440, 2880, 1080, true ) );
System.out.println( "2880*" + clacAspectRatio( 1080, 1920, 2880, false ) );
System.out.println( "1440*" + clacAspectRatio( 1080, 1920, 1440, true ) );
}
//处理结果
//==> 1920*960
//==> 1080*2160
//==> 2880*1620
//==> 1440*2560