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