CommonUtil.java
13.3 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package com.prws.common.utils;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.Rect;
import android.media.ExifInterface;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import com.prws.common.bean.CutPicBean;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.Random;
/**
* 获得屏幕相关的辅助类
*/
public class CommonUtil {
private CommonUtil() {
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* sp或者 dp 装换为 px
*/
public static int dpToPx(Context context, int dpValue) {
float scale = context.getResources().getDisplayMetrics().density;
return Math.round(dpValue * scale);
}
/**
* 获得屏幕宽度
*
* @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 = null;
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 = null;
bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
return bp;
}
public static PackageInfo getPackageInfo(Context context) {
PackageInfo pi = null;
try {
PackageManager pm = context.getPackageManager();
pi = pm.getPackageInfo(context.getPackageName(),
PackageManager.GET_CONFIGURATIONS);
return pi;
} catch (Exception e) {
e.printStackTrace();
}
return pi;
}
//相乘
public static double mul(double a1, double b1) {
BigDecimal a2 = new BigDecimal(Double.toString(a1));
BigDecimal b2 = new BigDecimal(Double.toString(b1));
return a2.multiply(b2).doubleValue();
}
//相乘
public static int mulForInt(double a1, double b1) {
BigDecimal a2 = new BigDecimal(Double.toString(a1));
BigDecimal b2 = new BigDecimal(Double.toString(b1));
return a2.multiply(b2).intValue();
}
//相除
public static double div(double a1, double b1, int scale) {
//scale参数为除不尽时,指定精度。
if (scale < 0) {
throw new IllegalArgumentException("error");
}
BigDecimal a2 = new BigDecimal(Double.toString(a1));
BigDecimal b2 = new BigDecimal(Double.toString(b1));
return a2.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
//相除
public static int divForInt(double a1, double b1, int scale) {
//scale参数为除不尽时,指定精度。
if (scale < 0) {
throw new IllegalArgumentException("error");
}
BigDecimal a2 = new BigDecimal(Double.toString(a1));
BigDecimal b2 = new BigDecimal(Double.toString(b1));
return a2.divide(b2, scale, BigDecimal.ROUND_HALF_UP).intValue();
}
public static String getStr() {
String str = new String();
Random r = new Random();
for (int i = 0; i < 5; i++) {
int temp = r.nextInt(58) + 65;
// 随机生成 65—122 的数(A—z)
if ((temp >= 'A' && temp <= 'Z') || (temp >= 'a' && temp <= 'z')) {
// 判断随机数是不是(A-Z)||(a-z)
str += (char) temp;
// 将当前随机数强制转化为字符类型并和字符串相加
} else {
// 不满足条件,将当前的i再执行一次
i--;
}
}
return str;
// 返回长度为5的随机字符串,字符串由随机的5个大小写字母组成
}
/**
* @param bmp
* @param uri
* @return 保存bitmap到指定路径
*/
public static boolean saveBitmapToUri(Bitmap bmp, String uri) {
final File imgFile = new File(uri);
if (!imgFile.exists()) {
imgFile.getParentFile().mkdir();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(imgFile);
bmp.compress(Bitmap.CompressFormat.JPEG, 85, fos);
fos.flush();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
public static String formatTime(Long time) {
Long seconds = time / 1000;
String standardTime;
if (seconds <= 0) {
standardTime = "00:00";
} else if (seconds < 60) {
standardTime = String.format(Locale.getDefault(), "00:%02d", seconds % 60);
} else if (seconds < 3600) {
standardTime = String.format(Locale.getDefault(), "%02d:%02d", seconds / 60, seconds % 60);
} else {
standardTime = String.format(Locale.getDefault(), "%02d:%02d:%02d", seconds / 3600, seconds % 3600 / 60, seconds % 60);
}
return standardTime;
}
public static String formatTimeChinese(Long time) {
Long seconds = time / 1000;
String standardTime;
if (seconds <= 0) {
standardTime = "00:00";
} else if (seconds < 60) {
standardTime = String.format(Locale.getDefault(), "%02d秒", seconds % 60);
} else if (seconds < 3600) {
standardTime = String.format(Locale.getDefault(), "%02d分%02d秒", seconds / 60, seconds % 60);
} else {
standardTime = String.format(Locale.getDefault(), "%02d时%02d分%02d秒", seconds / 3600, seconds % 3600 / 60, seconds % 60);
}
return standardTime;
}
public static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
//旋转图片 动作
Matrix matrix = new Matrix();
matrix.postRotate(angle);
// 创建新的图片
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
public static List<String> getYears(int num) {
List<String> list = new ArrayList<>();
list.add("全部");
Calendar cd = Calendar.getInstance();
int year = cd.get(Calendar.YEAR);
for (int i = 0; i < num; i++) {
list.add(Integer.toString(year));
year--;
}
return list;
}
public static int[] getRawScreenSize(Context context) {
int[] size = new int[2];
WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display d = w.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
d.getMetrics(metrics);
// since SDK_INT = 1;
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
try {
widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
} catch (Exception ignored) {
}
// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 17)
try {
Point realSize = new Point();
Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);
widthPixels = realSize.x;
heightPixels = realSize.y;
} catch (Exception ignored) {
}
size[0] = widthPixels;
size[1] = heightPixels;
return size;
}
public static List<Rect> getRect(List<CutPicBean.FigLoaction> list, Bitmap bitmap, int height, int width) {
List<Rect> rects = new ArrayList<>();
for (CutPicBean.FigLoaction figLoaction : list) {
Rect rect = new Rect();
rect.left = figLoaction.getPoints().get(0).getX() * width / bitmap.getWidth();
rect.top = figLoaction.getPoints().get(0).getY() * height / bitmap.getHeight();
rect.right = figLoaction.getPoints().get(2).getX() * width / bitmap.getWidth();
rect.bottom = figLoaction.getPoints().get(2).getY() * height / bitmap.getHeight();
rects.add(rect);
}
return rects;
}
public static Rect getRect(CutPicBean.FigLoaction figLoaction, Bitmap bitmap, int height, int width) {
Rect rect = new Rect();
rect.left = figLoaction.getPoints().get(0).getX() * width / bitmap.getWidth();
rect.top = figLoaction.getPoints().get(0).getY() * height / bitmap.getHeight();
rect.right = figLoaction.getPoints().get(2).getX() * width / bitmap.getWidth();
rect.bottom = figLoaction.getPoints().get(2).getY() * height / bitmap.getHeight();
return rect;
}
public static List<CutPicBean.FigLoaction> rotaingLocation(int angle, List<CutPicBean.FigLoaction> list, int width, int height) {
for (CutPicBean.FigLoaction figLoaction : list) {
for (CutPicBean.Points points : figLoaction.getPoints()) {
int x = points.getX();
int y = points.getY();
if (angle == 90) {
points.setX(height - y);
points.setY(x);
} else if (angle == 180) {
points.setX(width - x);
points.setY(height - y);
} else if (angle == 270) {
points.setX(y);
points.setY(width - x);
}
}
}
return list;
}
}