packagecom.zhagyu.opencut.utils;importandroid.content.res.Resources;publicclassSizeUtils{/**
* 在一个可用的区域内,计算出一个比例为 ratio 的最大的宽高。
*
* @param ratio ratio = (float) srcHeight / (float) srcWidth;
* @param maxWidth 可用的最大区域的宽度
* @param maxHeight 可用的最大区域的高度
* @return
*/publicstaticint[]getMaxSize(float ratio,int maxWidth,int maxHeight){int width =0;int height =0;for(int i =0; i <20; i++){
width =(int)(maxWidth *(1- i *0.05));
height =(int)(width * ratio);if(height < maxHeight){break;}}returnnewint[]{width, height};}/**
* Value of dp to value of px.
*
* @param dpValue The value of dp.
* @return value of px
*/publicstaticintdp2px(finalfloat dpValue){finalfloat scale =Resources.getSystem().getDisplayMetrics().density;return(int)(dpValue * scale +0.5f);}/**
* Value of px to value of dp.
*
* @param pxValue The value of px.
* @return value of dp
*/publicstaticintpx2dp(finalfloat pxValue){finalfloat scale =Resources.getSystem().getDisplayMetrics().density;return(int)(pxValue / scale +0.5f);}}