您的位置:首页 > 其它

关于显示的Util

2017-04-13 15:55 218 查看
public class DisplayUtil {
/**
* 将px值转换为dip或dp值,保证尺寸大小不变
*
* @param pxValue scale
*                (DisplayMetrics类中属性density)
* @return
*/
public static int px2dip(float pxValue) {
final float scale = x.app().getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}

/**
* 将dip或dp值转换为px值,保证尺寸大小不变
*
* @param dipValue scale
*                 (DisplayMetrics类中属性density)
* @return
*/
public static int dip2px(float dipValue) {
final float scale = x.app().getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}

/**
* 将px值转换为sp值,保证文字大小不变
*
* @param pxValue fontScale
*                (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int px2sp(float pxValue) {
final float fontScale = x.app().getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}

/**
* 将sp值转换为px值,保证文字大小不变
*
* @param spValue fontScale
*                (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int sp2px(float spValue) {
final float fontScale = x.app().getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}

//
//手机屏幕尺寸
//
public static String getScreenSizeOfDevice(Activity context) {

int w=context.getWindowManager().getDefaultDisplay().getWidth();
int h=context.getWindowManager().getDefaultDisplay().getHeight();
float ydpi=x.app().getResources().getDisplayMetrics().ydpi;
float xdpi= x.app().getResources().getDisplayMetrics().xdpi;
float hInches=h/ydpi;
float wInches=w/xdpi;
double screenInches =Math.sqrt(Math.pow(hInches, 2) + Math.pow(wInches, 2));

return (screenInches + "");
}

/**
* ListView已到顶部的判断
* @param listView
* @return
*/
public static boolean isListViewReachTopEdge(final ListView listView) {
boolean result=false;
if(listView.getFirstVisiblePosition()==0){
final View topChildView = listView.getChildAt(0);
result=topChildView.getTop()==0;
}
return result ;
}

/**
* ListView已到底部的判断
* @param listView
* @return
*/
public static boolean isListViewReachBottomEdge(final ListView listView) {
boolean result=false;
if (listView.getLastVisiblePosition() == (listView.getCount() - 1)) {
final View bottomChildView = listView.getChildAt(listView.getLastVisiblePosition() - listView.getFirstVisiblePosition());
result= (listView.getHeight()>=bottomChildView.getBottom());
};
return  result;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dp px