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;
}
}