Android Utils (不定期更新)

2017

11-14

ToastUtils

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import android.content.Context;
import android.widget.Toast;

public class ToastUtils {

// 是否需要显示 Toast,可以在 pplication 的 onCreate 函数里面初始化
public static boolean isShow = true;

/**
* 短时间显示Toast
* @param context
* @param message
*/

public static void showShort(Context context, String message) {
if (isShow) Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}

/**
* 长时间显示Toast
* @param context
* @param message
*/

public static void showLong(Context context, String message) {
if (isShow) Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
}

LogUtils

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import android.util.Log;

/**
* Log 统一管理类
*/

public class LogUtils {

// 是否需要打印log,可以在 application 的 onCreate 函数里面初始化
public static boolean isDebug = true;

public static void i(String tag, String msg) {
if (isDebug) Log.i(tag, msg);
}

public static void d(String tag, String msg) {
if (isDebug) Log.d(tag, msg);
}

public static void e(String tag, String msg) {
if (isDebug) Log.e(tag, msg);
}

public static void v(String tag, String msg) {
if (isDebug) Log.v(tag, msg);
}
}

ScreenUtils

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

public class ScreenUtils {

/**
* 获得屏幕宽度
*
* @param context
* @return
*/

public static int getScreenWidth(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}

/**
* 获得屏幕高度
*
* @param context
* @return
*/

public static int getScreenHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.heightPixels;
}

/**
* 获得状态栏的高度
* @param context
* @return
*/

public static int getStatusHeight(Context context) {
int statusHeight = -1;
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
statusHeight = context.getResources().getDimensionPixelSize(height);
} catch (Exception e) {
e.printStackTrace();
}
return statusHeight;
}

/**
* 获取当前屏幕截图,包含状态栏
* @param activity
* @return
*/

public static Bitmap snapShotWithStatusBar(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
int width = getScreenWidth(activity);
int height = getScreenHeight(activity);
Bitmap bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
view.destroyDrawingCache();
return bp;
}

/**
* 获取当前屏幕截图,不包含状态栏
* @param activity
* @return
*/

public static Bitmap snapShotWithoutStatusBar(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int width = getScreenWidth(activity);
int height = getScreenHeight(activity);
Bitmap bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache();
return bp;
}
}

DensityUtils

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import android.content.Context;
import android.util.TypedValue;

public class DensityUtils {

/**
* dp转px
* @param context
* @param dpVal
* @return
*/

public static int dp2px(Context context, float dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, context.getResources().getDisplayMetrics());
}

/**
* px转dp
* @param context
* @param pxVal
* @return
*/

public static float px2dp(Context context, float pxVal) {
final float scale = context.getResources().getDisplayMetrics().density;
return (pxVal / scale);
}

/**
* sp转px
* @param context
* @param spVal
* @return
*/

public static int sp2px(Context context, float spVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, context.getResources().getDisplayMetrics());
}

/**
* px转sp
* @param context
* @param pxVal
* @return
*/

public static float px2sp(Context context, float pxVal) {
return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
}
}