废话不多说直接上代码:
1.dip2px dp转px 无context算法
public static int px2dip(int pxValue)
{
final float scale = Resources.getSystem().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
public static float dip2px(float dipValue)
{
final float scale = Resources.getSystem().getDisplayMetrics().density;
return (dipValue * scale + 0.5f);
}2.获取屏幕区域
/**
* 获取屏幕区域
*/
public static Rect getScreenRect() {
DisplayMetrics displayMetric = new DisplayMetrics();
displayMetric = Resources.getSystem().getDisplayMetrics();
Rect rect = new Rect(0, 0, displayMetric.widthPixels, displayMetric.heightPixels);
return rect;
}
/**
* 获取屏幕宽度
*
*/
public static int getScreenWidth() {
return getScreenRect().width();
}
/**
* 获取屏幕高度
*
*/
public static int getScreenHeight() {
return getScreenRect().height();
}
Android中dip(dp)与px之间单位转换 dip2px dp转px 无context算法(以及获取获取屏幕宽度和高度)
原文地址:http://blog.csdn.net/weizongwei5/article/details/43667899