- Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathScreenshotUtils.java
More file actions
Latest commit
221 lines (198 loc) · 7.33 KB
/
Copy pathScreenshotUtils.java
File metadata and controls
221 lines (198 loc) · 7.33 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
packagecom.realmo.utils;
importandroid.app.Activity;
importandroid.graphics.Bitmap;
importandroid.graphics.Canvas;
importandroid.graphics.Rect;
importandroid.os.Environment;
importandroid.support.v7.widget.RecyclerView;
importandroid.text.TextUtils;
importandroid.util.Log;
importandroid.view.View;
importandroid.widget.ListView;
importandroid.widget.ScrollView;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.Locale;
/**
* 代码截屏工具 修改
*
*
* //保存后发送广播 弹出提示
* private void saveSuccess(File file){
* Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
* Uri uri = Uri.fromFile(file);
* intent.setData(uri);
* sendBroadcast(intent);
* ToastUtil.initToast(this, "已保存");
* }
*
*
*/
publicclassScreenshotUtils {
// 程序入口 截取当前屏幕
publicstaticFileshootLoacleView(Activitya, Stringpicpath) {
returnScreenshotUtils.savePic(ScreenshotUtils.takeScreenShot(a), picpath);
}
// 程序入口 截取ScrollView
publicstaticFileshootScrollView(ScrollViewscrollView, Stringpicpath) {
returnScreenshotUtils.savePic(getScrollViewBitmap(scrollView, picpath), picpath);
}
// 程序入口 截取ListView
publicstaticFileshootListView(ListViewlistView, Stringpicpath) {
returnScreenshotUtils.savePic(getListViewBitmap(listView, picpath), picpath);
}
// 程序入口 截取ListView
publicstaticFileshootRecyclerView(RecyclerViewrecyclerView, Stringpicpath, intrawNum) {
returnScreenshotUtils.savePic(getRecyclerViewBitmap(recyclerView, picpath, rawNum), picpath);
}
//获取默认存储地址 参数要传空
privatestaticStringgetDefaultPath() {
SimpleDateFormatsdf24H = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault());
Stringformat = sdf24H.format(newDate(System.currentTimeMillis()));
returnEnvironment.getExternalStorageDirectory().getAbsolutePath() + "/img/" + format + ".png";
}
// 获取指定Activity的截屏,保存到png文件
publicstaticBitmaptakeScreenShot(Activityactivity) {
// View是你需要截图的View
Viewview = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmapb1 = view.getDrawingCache();
// 获取状态栏高度
Rectframe = newRect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
intstatusBarHeight = frame.top;
System.out.println(statusBarHeight);
// 获取屏幕长和高
intwidth = activity.getWindowManager().getDefaultDisplay().getWidth();
intheight = activity.getWindowManager().getDefaultDisplay()
.getHeight();
// 去掉标题栏
// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
Bitmapb = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
returnb;
}
// 保存到sdcard
publicstaticFilesavePic(Bitmapb, StringstrFileName) {
Filefile = newFile(TextUtils.isEmpty(strFileName) ? getDefaultPath() : strFileName);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}
try {
BufferedOutputStreambos = newBufferedOutputStream(newFileOutputStream(file));
b.compress(Bitmap.CompressFormat.PNG, 100, bos);
bos.flush();
bos.close();
} catch (IOExceptione) {
e.printStackTrace();
}
returnfile;
}
/**
* 截取scrollview的屏幕 默认
**/
publicstaticBitmapgetScrollViewBitmap(ScrollViewscrollView, Stringpicpath) {
inth = 0;
Bitmapbitmap;
// 获取listView实际高度
for (inti = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
}
Log.d(TAG, "实际高度:" + h);
Log.d(TAG, " 高度:" + scrollView.getHeight());
// 创建对应大小的bitmap
bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
Bitmap.Config.ARGB_8888);
finalCanvascanvas = newCanvas(bitmap);
scrollView.draw(canvas);
// 测试输出
FileOutputStreamout = null;
try {
out = newFileOutputStream(TextUtils.isEmpty(picpath) ? getDefaultPath() : picpath);
} catch (FileNotFoundExceptione) {
e.printStackTrace();
}
try {
if (null != out) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
} catch (IOExceptione) {
}
returnbitmap;
}
privatestaticStringTAG = "Listview and ScrollView item 截图:";
/**
* 截图listview
**/
publicstaticBitmapgetListViewBitmap(ListViewlistView, Stringpicpath) {
inth = 0;
Bitmapbitmap;
// 获取listView实际高度
for (inti = 0; i < listView.getChildCount(); i++) {
h += listView.getChildAt(i).getHeight();
}
Log.e(TAG, "实际高度:" + h);
Log.e(TAG, "list 高度:" + listView.getHeight());
// 创建对应大小的bitmap
bitmap = Bitmap.createBitmap(listView.getWidth(), h,
Bitmap.Config.ARGB_8888);
finalCanvascanvas = newCanvas(bitmap);
listView.draw(canvas);
// 测试输出
FileOutputStreamout = null;
try {
out = newFileOutputStream(TextUtils.isEmpty(picpath) ? getDefaultPath() : picpath);
} catch (FileNotFoundExceptione) {
e.printStackTrace();
}
try {
if (null != out) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
} catch (IOExceptione) {
}
returnbitmap;
}
privatestaticBitmapgetRecyclerViewBitmap(RecyclerViewrecyclerView, Stringpicpath, introwNum) {
inth = 0;
Bitmapbitmap;
// 获取listView实际高度
for (inti = 0; i < recyclerView.getChildCount() / rowNum; i++) {
h += recyclerView.getChildAt(i).getHeight();
}
Log.e(TAG, "实际高度:" + h);
Log.e(TAG, "list 高度:" + recyclerView.getHeight());
// 创建对应大小的bitmap
bitmap = Bitmap.createBitmap(recyclerView.getWidth(), h,
Bitmap.Config.ARGB_8888);
finalCanvascanvas = newCanvas(bitmap);
recyclerView.draw(canvas);
// 测试输出
FileOutputStreamout = null;
try {
out = newFileOutputStream(TextUtils.isEmpty(picpath) ? getDefaultPath() : picpath);
} catch (FileNotFoundExceptione) {
e.printStackTrace();
}
try {
if (null != out) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
} catch (IOExceptione) {
}
returnbitmap;
}
}