1. dp , px 的转化 sp,px 的转化

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
public class PxUtil {

/**
* 得到设备的密度
*/
public static float getScreenDensity(Context context) {
return context.getResources().getDisplayMetrics().density;
}

/**
* 将传递的 整型dp 值转化为 px
* @param dp
* @param context
* @return
*/
public static int dpToPx(int dp, Context context){
return (int) (dp * getScreenDensity(context));
}

/**
* 将传递的 浮点型dp 值转化为 px
* @param dp
* @param context
* @return
*/
public static int dpToPx(float dp, Context context){
return (int) (dp * getScreenDensity(context));
}

/**
* 将传递的 整型px 值转化为 dp
* @param px
* @param context
* @return
*/
public static float pxToDp(int px, Context context){
return (px / getScreenDensity(context));
}

/**
* 将传递的 浮点型px 值转化为 dp
* @param px
* @param context
* @return
*/
public static float pxToDp(float px, Context context){
return (px / getScreenDensity(context));
}

/**
* 将传递的 sp 值转化为 px
* @param sp
* @param context
* @return
*/
public static int spToPx(int sp, Context context){
return (int) (sp * getScreenDensity(context));
}

/**
* 将传递的 px 值 转化为 sp
* @param px
* @param context
* @return
*/
public static int pxToSp(int px, Context context){
return (int) (px / getScreenDensity(context));
}
}

2. 屏幕宽度,高度,状态栏高度,导航栏高度

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
92
93
94
95
96
97
98
99
100
101
102
public class ScreenUtil {
/**
* 获取屏幕的宽度
* @param context
* @return
*/
public static int getScreenWidth(Context context){
//窗口管理者
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//存储尺寸的
DisplayMetrics displayMetrics = new DisplayMetrics();
//获取尺寸
assert windowManager != null;
windowManager.getDefaultDisplay().getRealMetrics(displayMetrics);

return displayMetrics.widthPixels;
}

/**
* 获取屏幕的高度
* @param context
* @return
*/
public static int getScreenHeight(Context context){
//窗口管理者
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//存储尺寸的
DisplayMetrics displayMetrics = new DisplayMetrics();
//获取尺寸
assert windowManager != null;
windowManager.getDefaultDisplay().getRealMetrics(displayMetrics);

return displayMetrics.heightPixels;
}

/**
* 获取StatusBar状态栏的高度
* @param context
* @return
*/
public static int getStateBarHeight(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;
}

/**
* 判断设置是否有NavigationBar导航栏
* @param context
* @return
*/
private static boolean checkDeviceHasNavigationBar(Context context) {
boolean hasNavigationBar = false;
Resources rs = context.getResources();
int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
if (id > 0) {
hasNavigationBar = rs.getBoolean(id);
}
try {
Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
Method m = systemPropertiesClass.getMethod("get", String.class);
String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
if ("1".equals(navBarOverride)) {
hasNavigationBar = false;
} else if ("0".equals(navBarOverride)) {
hasNavigationBar = true;
}
} catch (Exception e) {

}

return hasNavigationBar;
}

/**
* 返回NavigationBar导航栏的高度
* @param context
* @return
*/
public static int getNavigationBarHeight(Context context) {
int height = 0;

//如果存在
if (ScreenUtil.checkDeviceHasNavigationBar(context)){
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android");
height = resources.getDimensionPixelSize(resourceId);
Log.v("dbw", "Navi height:" + height);
}

return height;
}

}

3. 程序是否在后台

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
public class StateUtil {

/**
* 判断是否在前台
* @param context
* @return
*/
public static boolean isBackground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(context.getPackageName())) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
Log.i("后台", appProcess.processName);
return true;
}else{
Log.i("前台", appProcess.processName);
return false;
}
}
}

return false;
}
}

4. 将一个bitmap保存到系统相册中

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
public class SaveToAlbumUtil {
/**
* 将一个bitmap保存到系统相册中
* @param bitmap
* @param picName
* @param context
*/
public static void saveBitmapToGallery(Bitmap bitmap, String picName, Context context) {

String fileName = null;
//系统相册目录
String galleryPath= Environment.getExternalStorageDirectory()
+ File.separator + Environment.DIRECTORY_DCIM
+File.separator+"Camera"+File.separator;

// 声明文件对象
File file = null;
// 声明输出流
FileOutputStream outStream = null;

try {
// 如果有目标文件,直接获得文件对象,否则创建一个以filename为名称的文件
file = new File(galleryPath, picName+ ".jpeg");

// 获得文件相对路径
fileName = file.toString();
// 获得输出流,如果文件中有内容,追加内容
outStream = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outStream);

} catch (Exception e) {
e.getStackTrace();
}finally {
//释放资源
try {
if (outStream != null) {
outStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

//将图片插入到系统相册中
MediaStore.Images.Media.insertImage(context.getContentResolver(),
bitmap, fileName, null);

//通知相册更新
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
context.sendBroadcast(intent);

//信息提示
Toast.makeText(context, "图片保存成功", Toast.LENGTH_SHORT).show();
}
}