| | |
| | | |
| | | public class DimenUtils { |
| | | |
| | | public static int dipToPixels(int dip, Context context) { |
| | | Resources r = context.getResources(); |
| | | float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, |
| | | r.getDisplayMetrics()); |
| | | return (int) px; |
| | | } |
| | | public static int dipToPixels(int dip, Context context) { |
| | | Resources r = context.getResources(); |
| | | float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, |
| | | r.getDisplayMetrics()); |
| | | return (int) px; |
| | | } |
| | | |
| | | public static int pxToDip(int px, Context context) { |
| | | Resources r = context.getResources(); |
| | | // float dip = TypedValue.complexToDimensionPixelSize(data, metrics) |
| | | // TypedValue.complexToDimensionPixelOffset(data, metrics) |
| | | return 0; |
| | | } |
| | | public static int pxToDip(int px, Context context) { |
| | | Resources r = context.getResources(); |
| | | // float dip = TypedValue.complexToDimensionPixelSize(data, metrics) |
| | | // TypedValue.complexToDimensionPixelOffset(data, metrics) |
| | | return 0; |
| | | } |
| | | |
| | | public static int spToPixels(int sp, Context context) { |
| | | Resources r = context.getResources(); |
| | | float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, |
| | | r.getDisplayMetrics()); |
| | | return (int) px; |
| | | } |
| | | public static int spToPixels(int sp, Context context) { |
| | | Resources r = context.getResources(); |
| | | float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, |
| | | r.getDisplayMetrics()); |
| | | return (int) px; |
| | | } |
| | | |
| | | public static int getScreenWidth(Activity activity) { |
| | | DisplayMetrics metric = new DisplayMetrics(); |
| | | activity.getWindowManager().getDefaultDisplay().getMetrics(metric); |
| | | return metric.widthPixels; |
| | | } |
| | | private static int deviceWidth = 0; |
| | | |
| | | public static int getScreenHeight(Activity activity) { |
| | | DisplayMetrics metric = new DisplayMetrics(); |
| | | activity.getWindowManager().getDefaultDisplay().getMetrics(metric); |
| | | return metric.heightPixels; |
| | | } |
| | | public static int getScreenWidth(Activity activity) { |
| | | if (deviceWidth == 0) { |
| | | DisplayMetrics metric = new DisplayMetrics(); |
| | | activity.getWindowManager().getDefaultDisplay().getMetrics(metric); |
| | | deviceWidth = metric.widthPixels; |
| | | } |
| | | return deviceWidth; |
| | | } |
| | | |
| | | /** |
| | | * 根据手机的分辨率从 dp 的单位 转成为 px(像素) |
| | | */ |
| | | public static int dip2px(Context context, float dpValue) { |
| | | final float scale = context.getResources().getDisplayMetrics().density; |
| | | return (int) (dpValue * scale + 0.5f); |
| | | } |
| | | public static int getScreenHeight(Activity activity) { |
| | | DisplayMetrics metric = new DisplayMetrics(); |
| | | activity.getWindowManager().getDefaultDisplay().getMetrics(metric); |
| | | return metric.heightPixels; |
| | | } |
| | | |
| | | /** |
| | | * 根据手机的分辨率从 px(像素) 的单位 转成为 dp |
| | | */ |
| | | public static int px2dip(Context context, float pxValue) { |
| | | final float scale = context.getResources().getDisplayMetrics().density; |
| | | return (int) (pxValue / scale + 0.5f); |
| | | } |
| | | /** |
| | | * 根据手机的分辨率从 dp 的单位 转成为 px(像素) |
| | | */ |
| | | public static int dip2px(Context context, float dpValue) { |
| | | final float scale = context.getResources().getDisplayMetrics().density; |
| | | return (int) (dpValue * scale + 0.5f); |
| | | } |
| | | |
| | | /** |
| | | * 根据手机的分辨率从 px(像素) 的单位 转成为 dp |
| | | */ |
| | | public static int px2dip(Context context, float pxValue) { |
| | | final float scale = context.getResources().getDisplayMetrics().density; |
| | | return (int) (pxValue / scale + 0.5f); |
| | | } |
| | | |
| | | /** |
| | | * 获取状态栏高度 |
| | | * |
| | | * @param context |
| | | * @return |
| | | */ |
| | | public static int getStatusBarHeight(Context context) { |
| | | Resources resources = context.getResources(); |
| | | int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android"); |
| | | int height = resources.getDimensionPixelSize(resourceId); |
| | | return height; |
| | | } |
| | | } |