|
|
@@ -1,9 +1,96 @@
|
|
|
package com.me.imageloader.utils;
|
|
|
|
|
|
+import android.app.Activity;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.res.TypedArray;
|
|
|
+import android.util.DisplayMetrics;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+
|
|
|
/**
|
|
|
- * Created by FANGlh on 2017/9/6.
|
|
|
- * function:
|
|
|
+ * 屏幕相关工具类
|
|
|
+ *
|
|
|
+ * Created by Arison on 2017/5/23.
|
|
|
*/
|
|
|
-
|
|
|
public class ScreenUtil {
|
|
|
+
|
|
|
+ public static int dip2px( float dpValue) {
|
|
|
+ final float scale = Utils.getContext().getResources().getDisplayMetrics().density;
|
|
|
+ return (int) (dpValue * scale + 0.5f);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static int px2dip( float pxValue) {
|
|
|
+ final float scale = Utils.getContext().getResources().getDisplayMetrics().density;
|
|
|
+ return (int) (pxValue / scale + 0.5f);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int px2sp( float pxValue) {
|
|
|
+ final float scale = Utils.getContext().getResources().getDisplayMetrics().density;
|
|
|
+ return (int) (pxValue / scale + 0.5f);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int sp2px(float spValue) {
|
|
|
+ final float scale = Utils.getContext().getResources().getDisplayMetrics().density;
|
|
|
+ return (int) (spValue * scale + 0.5f);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int getScreenWidth(Context context) {
|
|
|
+ DisplayMetrics localDisplayMetrics = new DisplayMetrics();
|
|
|
+ ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
|
|
|
+ return localDisplayMetrics.widthPixels;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int getScreenHeight(Context context) {
|
|
|
+ DisplayMetrics localDisplayMetrics = new DisplayMetrics();
|
|
|
+ ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
|
|
|
+ return localDisplayMetrics.heightPixels - getStatusBarHeight(context);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @deprecated use {@link #getScreenHeight(Context)} instead
|
|
|
+ * get screen size height in pixels
|
|
|
+ *
|
|
|
+ * @param context
|
|
|
+ * @return the pixels of the screen height
|
|
|
+ */
|
|
|
+ public static int getHeight(Activity context) {
|
|
|
+ DisplayMetrics dm = new DisplayMetrics();
|
|
|
+ context.getWindowManager().getDefaultDisplay().getMetrics(dm);
|
|
|
+ return dm.heightPixels;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int getStatusBarHeight(Context context){
|
|
|
+ Class<?> c = null;
|
|
|
+ Object obj = null;
|
|
|
+ Field field = null;
|
|
|
+ int x = 0, statusBarHeight = 0;
|
|
|
+ try {
|
|
|
+ c = Class.forName("com.android.internal.R$dimen");
|
|
|
+ obj = c.newInstance();
|
|
|
+ field = c.getField("status_bar_height");
|
|
|
+ x = Integer.parseInt(field.get(obj).toString());
|
|
|
+ statusBarHeight = context.getResources().getDimensionPixelSize(x);
|
|
|
+ } catch (Exception e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ return statusBarHeight;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * get toolbar height
|
|
|
+ *
|
|
|
+ * @param context
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int getToolbarHeight(Context context) {
|
|
|
+ final TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(
|
|
|
+ new int[]{android.R.attr.actionBarSize});
|
|
|
+ int toolbarHeight = (int) styledAttributes.getDimension(0, 0);
|
|
|
+ styledAttributes.recycle();
|
|
|
+
|
|
|
+ return toolbarHeight;
|
|
|
+ }
|
|
|
}
|