/
CapturePictureUtils.java
1519 lines (1414 loc) · 60 KB
/
CapturePictureUtils.java
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package dev.utils.app;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import androidx.annotation.ColorInt;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import dev.utils.LogPrintUtils;
import dev.utils.app.image.BitmapUtils;
import dev.utils.common.ArrayUtils;
import dev.utils.common.NumberUtils;
/**
* detail: 截图工具类
* @author Ttt
* <pre>
* 截图
* @see <a href="https://www.cnblogs.com/angel88/p/7933437.html"/>
* WebView 截长图解决方案
* @see <a href="https://www.jianshu.com/p/0faa70e88441"/>
* X5 WebView 使用 snapshotWholePage 方法清晰截图
* @see <a href="https://www.v2ex.com/t/583020"/>
* 针对 SurfaceView、Surface、Window 截图可使用
* {@link android.view.PixelCopy} 进行实现
* </pre>
*/
public final class CapturePictureUtils {
private CapturePictureUtils() {
}
// 日志 TAG
private static final String TAG = CapturePictureUtils.class.getSimpleName();
// Bitmap Config
private static Bitmap.Config BITMAP_CONFIG = Bitmap.Config.RGB_565;
// Canvas 背景色
private static int BACKGROUND_COLOR = Color.TRANSPARENT;
// 画笔
private static Paint PAINT = new Paint();
// ==========
// = 配置相关 =
// ==========
/**
* 设置 Bitmap Config
* @param config {@link Bitmap.Config}
*/
public static void setBitmapConfig(final Bitmap.Config config) {
if (config == null) return;
BITMAP_CONFIG = config;
}
/**
* 设置 Canvas 背景色
* @param backgroundColor 背景色
*/
public static void setBackgroundColor(@ColorInt final int backgroundColor) {
BACKGROUND_COLOR = backgroundColor;
}
/**
* 设置画笔
* @param paint {@link Paint}
*/
public static void setPaint(final Paint paint) {
if (paint == null) return;
PAINT = paint;
}
// =======
// = 截图 =
// =======
// ============
// = Activity =
// ============
/**
* 获取当前屏幕截图, 包含状态栏 ( 顶部灰色 TitleBar 高度, 没有设置 android:theme 的 NoTitleBar 时会显示 )
* @param activity {@link Activity}
* @return 当前屏幕截图, 包含状态栏
*/
public static Bitmap snapshotWithStatusBar(final Activity activity) {
try {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
// 重新创建绘图缓存, 此时的背景色是黑色
view.buildDrawingCache();
// 获取绘图缓存, 注意这里得到的只是一个图像的引用
Bitmap cacheBitmap = view.getDrawingCache();
if (cacheBitmap == null) return null;
// 获取屏幕宽度
int[] widthHeight = ScreenUtils.getScreenWidthHeight();
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
// 创建新的图片
Bitmap bitmap = Bitmap.createBitmap(
cacheBitmap, 0, 0,
widthHeight[0], widthHeight[1]
);
// 释放绘图资源所使用的缓存
view.destroyDrawingCache();
return bitmap;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "snapshotWithStatusBar");
}
return null;
}
/**
* 获取当前屏幕截图, 不包含状态栏 ( 如果 android:theme 全屏, 则截图无状态栏 )
* @param activity {@link Activity}
* @return 当前屏幕截图, 不包含状态栏
*/
public static Bitmap snapshotWithoutStatusBar(final Activity activity) {
try {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
// 重新创建绘图缓存, 此时的背景色是黑色
view.buildDrawingCache();
// 获取绘图缓存, 注意这里得到的只是一个图像的引用
Bitmap cacheBitmap = view.getDrawingCache();
if (cacheBitmap == null) return null;
// 获取屏幕宽度
int[] widthHeight = ScreenUtils.getScreenWidthHeight();
// 获取状态栏高度
int statusBarHeight = BarUtils.getStatusBarHeight();
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
// 创建新的图片
Bitmap bitmap = Bitmap.createBitmap(
cacheBitmap, 0, statusBarHeight, widthHeight[0],
widthHeight[1] - statusBarHeight
);
// 释放绘图资源所使用的缓存
view.destroyDrawingCache();
return bitmap;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "snapshotWithoutStatusBar");
}
return null;
}
// ===========
// = WebView =
// ===========
/**
* 关闭 WebView 优化
* <pre>
* 推荐在 setContentView 前调用
* {@link CapturePictureUtils#snapshotByWebView}
* </pre>
*/
public static void enableSlowWholeDocumentDraw() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
android.webkit.WebView.enableSlowWholeDocumentDraw();
}
}
/**
* 截图 WebView
* @param webView {@link WebView}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByWebView(final WebView webView) {
return snapshotByWebView(webView, Integer.MAX_VALUE, BITMAP_CONFIG, 0F);
}
/**
* 截图 WebView
* @param webView {@link WebView}
* @param maxHeight 最大高度
* @return {@link Bitmap}
*/
public static Bitmap snapshotByWebView(
final WebView webView,
final int maxHeight
) {
return snapshotByWebView(webView, maxHeight, BITMAP_CONFIG, 0F);
}
/**
* 截图 WebView
* @param webView {@link WebView}
* @param scale 缩放比例
* @return {@link Bitmap}
*/
public static Bitmap snapshotByWebView(
final WebView webView,
final float scale
) {
return snapshotByWebView(webView, Integer.MAX_VALUE, BITMAP_CONFIG, scale);
}
/**
* 截图 WebView
* @param webView {@link WebView}
* @param maxHeight 最大高度
* @param config {@link Bitmap.Config}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByWebView(
final WebView webView,
final int maxHeight,
final Bitmap.Config config
) {
return snapshotByWebView(webView, maxHeight, config, 0F);
}
/**
* 截图 WebView
* <pre>
* TODO
* 在 Android 5.0 及以上版本, Android 对 WebView 进行了优化, 为了减少内存使用和提高性能
* 使用 WebView 加载网页时只绘制显示部分, 如果我们不做处理, 就会出现只截到屏幕内显示的 WebView 内容, 其它部分是空白的情况
* 通过调用 WebView.enableSlowWholeDocumentDraw() 方法可以关闭这种优化, 但要注意的是, 该方法需要在 WebView 实例被创建前就要调用,
* 否则没有效果, 所以我们在 WebView 实例被创建前加入代码
* {@link CapturePictureUtils#enableSlowWholeDocumentDraw}
* </pre>
* @param webView {@link WebView}
* @param maxHeight 最大高度
* @param config {@link Bitmap.Config}
* @param scale 缩放比例
* @return {@link Bitmap}
*/
public static Bitmap snapshotByWebView(
final WebView webView,
final int maxHeight,
final Bitmap.Config config,
final float scale
) {
if (webView != null && config != null) {
// Android 5.0 以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
float newScale = scale;
if (newScale <= 0) {
// 该方法已抛弃, 可通过 setWebViewClient
// onScaleChanged(WebView view, float oldScale, float newScale)
// 存储并传入 newScale
newScale = webView.getScale();
}
int width = webView.getWidth();
int height = (int) (webView.getContentHeight() * newScale + 0.5);
// 重新设置高度
height = Math.min(height, maxHeight);
// 创建位图
Bitmap bitmap = Bitmap.createBitmap(width, height, config);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(BACKGROUND_COLOR);
webView.draw(canvas);
return bitmap;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "snapshotByWebView SDK_INT >= 21(5.0)");
}
} else {
try {
Picture picture = webView.capturePicture();
int width = picture.getWidth();
int height = picture.getHeight();
if (width > 0 && height > 0) {
// 重新设置高度
height = Math.min(height, maxHeight);
// 创建位图
Bitmap bitmap = Bitmap.createBitmap(width, height, config);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(BACKGROUND_COLOR);
picture.draw(canvas);
return bitmap;
}
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "snapshotByWebView SDK_INT < 21(5.0)");
}
}
}
return null;
}
// ========
// = View =
// ========
/**
* 通过 View 绘制为 Bitmap
* @param view {@link View}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByView(final View view) {
return snapshotByView(view, BITMAP_CONFIG);
}
/**
* 通过 View 绘制为 Bitmap
* <pre>
* 如果 View 是动态创建 ( 且没有添加到任何 ViewGroup 中 ) 想进行截图
* 需要设置完控件值, 如: text、background 等
* 再调用以下两个方法其中一个, 进行测量 View
* {@link WidgetUtils#measureView(View, int)}
* {@link WidgetUtils#measureView(View, int, int)}
* 接着调用截图方法
* {@link #snapshotByView(View)}
* {@link #snapshotByViewCache(View)}
* </pre>
* @param view {@link View}
* @param config {@link Bitmap.Config}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByView(
final View view,
final Bitmap.Config config
) {
if (view == null || config == null) return null;
try {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), config);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(BACKGROUND_COLOR);
view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
view.draw(canvas);
return bitmap;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "snapshotByView");
}
return null;
}
/**
* 通过 View Cache 绘制为 Bitmap
* <pre>
* 如果 View 是动态创建 ( 且没有添加到任何 ViewGroup 中 ) 想进行截图
* 需要设置完控件值, 如: text、background 等
* 再调用以下两个方法其中一个, 进行测量 View
* {@link WidgetUtils#measureView(View, int)}
* {@link WidgetUtils#measureView(View, int, int)}
* 接着调用截图方法
* {@link #snapshotByView(View)}
* {@link #snapshotByViewCache(View)}
* </pre>
* @param view {@link View}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByViewCache(final View view) {
if (view == null) return null;
try {
// 清除视图焦点
view.clearFocus();
// 将视图设为不可点击
view.setPressed(false);
// 获取视图是否可以保存画图缓存
boolean willNotCache = view.willNotCacheDrawing();
view.setWillNotCacheDrawing(false);
// 获取绘制缓存位图的背景颜色
int color = view.getDrawingCacheBackgroundColor();
// 设置绘图背景颜色
view.setDrawingCacheBackgroundColor(0);
if (color != 0) { // 获取的背景不是黑色的则释放以前的绘图缓存
view.destroyDrawingCache(); // 释放绘图资源所使用的缓存
}
// 重新创建绘图缓存, 此时的背景色是黑色
view.buildDrawingCache();
// 获取绘图缓存, 注意这里得到的只是一个图像的引用
Bitmap cacheBitmap = view.getDrawingCache();
if (cacheBitmap == null) return null;
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// 释放位图内存
view.destroyDrawingCache();
// 回滚以前的缓存设置、缓存颜色设置
view.setWillNotCacheDrawing(willNotCache);
view.setDrawingCacheBackgroundColor(color);
return bitmap;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "snapshotByViewCache");
}
return null;
}
// ================
// = LinearLayout =
// ================
/**
* 通过 LinearLayout 绘制为 Bitmap
* @param linearLayout {@link LinearLayout}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByLinearLayout(final LinearLayout linearLayout) {
return snapshotByLinearLayout(linearLayout, BITMAP_CONFIG);
}
/**
* 通过 LinearLayout 绘制为 Bitmap
* <pre>
* LinearLayout 容器中不能有诸如 ListView、GridView、WebView 这样的高度可变的控件
* </pre>
* @param linearLayout {@link LinearLayout}
* @param config {@link Bitmap.Config}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByLinearLayout(
final LinearLayout linearLayout,
final Bitmap.Config config
) {
return snapshotByView(linearLayout, config);
}
// ===============
// = FrameLayout =
// ===============
/**
* 通过 FrameLayout 绘制为 Bitmap
* @param frameLayout {@link FrameLayout}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByFrameLayout(final FrameLayout frameLayout) {
return snapshotByFrameLayout(frameLayout, BITMAP_CONFIG);
}
/**
* 通过 FrameLayout 绘制为 Bitmap
* <pre>
* FrameLayout 容器中不能有诸如 ListView、GridView、WebView 这样的高度可变的控件
* </pre>
* @param frameLayout {@link FrameLayout}
* @param config {@link Bitmap.Config}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByFrameLayout(
final FrameLayout frameLayout,
final Bitmap.Config config
) {
return snapshotByView(frameLayout, config);
}
// ==================
// = RelativeLayout =
// ==================
/**
* 通过 RelativeLayout 绘制为 Bitmap
* @param relativeLayout {@link RelativeLayout}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByRelativeLayout(final RelativeLayout relativeLayout) {
return snapshotByRelativeLayout(relativeLayout, BITMAP_CONFIG);
}
/**
* 通过 RelativeLayout 绘制为 Bitmap
* <pre>
* RelativeLayout 容器中不能有诸如 ListView、GridView、WebView 这样的高度可变的控件
* </pre>
* @param relativeLayout {@link RelativeLayout}
* @param config {@link Bitmap.Config}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByRelativeLayout(
final RelativeLayout relativeLayout,
final Bitmap.Config config
) {
return snapshotByView(relativeLayout, config);
}
// ==============
// = ScrollView =
// ==============
/**
* 通过 ScrollView 绘制为 Bitmap
* @param scrollView {@link ScrollView}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByScrollView(final ScrollView scrollView) {
return snapshotByScrollView(scrollView, BITMAP_CONFIG);
}
/**
* 通过 ScrollView 绘制为 Bitmap
* <pre>
* ScrollView 容器中不能有诸如 ListView、GridView、WebView 这样的高度可变的控件
* </pre>
* @param scrollView {@link ScrollView}
* @param config {@link Bitmap.Config}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByScrollView(
final ScrollView scrollView,
final Bitmap.Config config
) {
if (scrollView == null || config == null) return null;
try {
View view = scrollView.getChildAt(0);
int width = view.getWidth();
int height = view.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, config);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(BACKGROUND_COLOR);
scrollView.layout(0, 0, scrollView.getMeasuredWidth(),
scrollView.getMeasuredHeight()
);
scrollView.draw(canvas);
return bitmap;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "snapshotByScrollView");
}
return null;
}
// ========================
// = HorizontalScrollView =
// ========================
/**
* 通过 HorizontalScrollView 绘制为 Bitmap
* @param scrollView {@link HorizontalScrollView}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByHorizontalScrollView(final HorizontalScrollView scrollView) {
return snapshotByHorizontalScrollView(scrollView, BITMAP_CONFIG);
}
/**
* 通过 HorizontalScrollView 绘制为 Bitmap
* @param scrollView {@link HorizontalScrollView}
* @param config {@link Bitmap.Config}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByHorizontalScrollView(
final HorizontalScrollView scrollView,
final Bitmap.Config config
) {
if (scrollView == null || config == null) return null;
try {
View view = scrollView.getChildAt(0);
int width = view.getWidth();
int height = view.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, config);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(BACKGROUND_COLOR);
scrollView.layout(0, 0, scrollView.getMeasuredWidth(),
scrollView.getMeasuredHeight()
);
scrollView.draw(canvas);
return bitmap;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "snapshotByHorizontalScrollView");
}
return null;
}
// ====================
// = NestedScrollView =
// ====================
/**
* 通过 NestedScrollView 绘制为 Bitmap
* @param scrollView {@link NestedScrollView}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByNestedScrollView(final NestedScrollView scrollView) {
return snapshotByNestedScrollView(scrollView, BITMAP_CONFIG);
}
/**
* 通过 NestedScrollView 绘制为 Bitmap
* @param scrollView {@link NestedScrollView}
* @param config {@link Bitmap.Config}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByNestedScrollView(
final NestedScrollView scrollView,
final Bitmap.Config config
) {
if (scrollView == null || config == null) return null;
try {
View view = scrollView.getChildAt(0);
int width = view.getWidth();
int height = view.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, config);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(BACKGROUND_COLOR);
scrollView.layout(0, 0, scrollView.getMeasuredWidth(),
scrollView.getMeasuredHeight()
);
scrollView.draw(canvas);
return bitmap;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "snapshotByNestedScrollView");
}
return null;
}
// ============
// = ListView =
// ============
/**
* 通过 ListView 绘制为 Bitmap
* @param listView {@link ListView}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByListView(final ListView listView) {
return snapshotByListView(listView, BITMAP_CONFIG);
}
/**
* 通过 ListView 绘制为 Bitmap
* @param listView {@link ListView}
* @param config {@link Bitmap.Config}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByListView(
final ListView listView,
final Bitmap.Config config
) {
if (listView == null || config == null) return null;
try {
// Adapter
ListAdapter listAdapter = listView.getAdapter();
// Item 总条数
int itemCount = listAdapter.getCount();
// 没数据则直接跳过
if (itemCount == 0) return null;
// 高度
int height = 0;
// 获取子项间分隔符占用的高度
int dividerHeight = listView.getDividerHeight();
// View Bitmaps
Bitmap[] bitmaps = new Bitmap[itemCount];
// 循环绘制每个 Item 并保存 Bitmap
for (int i = 0; i < itemCount; i++) {
View childView = listAdapter.getView(i, null, listView);
WidgetUtils.measureView(childView, listView.getWidth());
bitmaps[i] = canvasBitmap(childView, config);
height += childView.getMeasuredHeight();
}
// 追加子项间分隔符占用的高度
height += (dividerHeight * (itemCount - 1));
int width = listView.getMeasuredWidth();
// 创建位图
Bitmap bitmap = Bitmap.createBitmap(width, height, config);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(BACKGROUND_COLOR);
// 累加高度
int appendHeight = 0;
for (Bitmap bmp : bitmaps) {
canvas.drawBitmap(bmp, 0, appendHeight, PAINT);
appendHeight += (bmp.getHeight() + dividerHeight);
// 释放资源
BitmapUtils.recycle(bmp);
}
return bitmap;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "snapshotByListView");
}
return null;
}
// ============
// = GridView =
// ============
/**
* 通过 GridView 绘制为 Bitmap
* @param gridView {@link GridView}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByGridView(final GridView gridView) {
return snapshotByGridView(gridView, BITMAP_CONFIG, false);
}
/**
* 通过 GridView 绘制为 Bitmap
* @param gridView {@link GridView}
* @param config {@link Bitmap.Config}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByGridView(
final GridView gridView,
final Bitmap.Config config
) {
return snapshotByGridView(gridView, config, false);
}
/**
* 通过 GridView 绘制为 Bitmap
* @param gridView {@link GridView}
* @param config {@link Bitmap.Config}
* @param listViewEffect 是否保存 ListView 效果 ( 每个 Item 铺满 )
* @return {@link Bitmap}
*/
public static Bitmap snapshotByGridView(
final GridView gridView,
final Bitmap.Config config,
final boolean listViewEffect
) {
if (gridView == null || config == null) return null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) return null;
try {
// Adapter
ListAdapter listAdapter = gridView.getAdapter();
// Item 总条数
int itemCount = listAdapter.getCount();
// 没数据则直接跳过
if (itemCount == 0) return null;
// 高度
int height = 0;
// 获取一共多少列
int numColumns = gridView.getNumColumns();
// 每列之间的间隔 |
int horizontalSpacing = gridView.getHorizontalSpacing();
// 每行之间的间隔 -
int verticalSpacing = gridView.getVerticalSpacing();
// View Bitmaps
Bitmap[] bitmaps = new Bitmap[itemCount];
// 效果处理 ( ListView 效果 Item 铺满 )
if (listViewEffect) {
// 循环绘制每个 Item 并保存 Bitmap
for (int i = 0; i < itemCount; i++) {
View childView = listAdapter.getView(i, null, gridView);
WidgetUtils.measureView(childView, gridView.getWidth());
bitmaps[i] = canvasBitmap(childView, config);
height += childView.getMeasuredHeight();
}
// 追加子项间分隔符占用的高度
height += (verticalSpacing * (itemCount - 1));
int width = gridView.getMeasuredWidth();
// 创建位图
Bitmap bitmap = Bitmap.createBitmap(width, height, config);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(BACKGROUND_COLOR);
// 累加高度
int appendHeight = 0;
for (Bitmap bmp : bitmaps) {
canvas.drawBitmap(bmp, 0, appendHeight, PAINT);
appendHeight += (bmp.getHeight() + verticalSpacing);
// 释放资源
BitmapUtils.recycle(bmp);
}
return bitmap;
} else {
// 获取倍数 ( 行数 )
int lineNumber = NumberUtils.multiple(itemCount, numColumns);
// 计算总共的宽度 (GridView 宽度 - 列分割间距 ) / numColumns
int childWidth = (gridView.getWidth() - (numColumns - 1) * horizontalSpacing) / numColumns;
// 记录每行最大高度
int[] rowHeightArrays = new int[lineNumber];
// 临时高度 ( 保存行中最高的列高度 )
int tempHeight;
// 循环每一行绘制每个 Item 并保存 Bitmap
for (int i = 0; i < lineNumber; i++) {
// 清空高度
tempHeight = 0;
// 循环列数
for (int j = 0; j < numColumns; j++) {
// 获取对应的索引
int position = i * numColumns + j;
// 小于总数才处理
if (position < itemCount) {
View childView = listAdapter.getView(position, null, gridView);
WidgetUtils.measureView(childView, childWidth);
bitmaps[position] = canvasBitmap(childView, config);
int itemHeight = childView.getMeasuredHeight();
// 保留最大高度
tempHeight = Math.max(itemHeight, tempHeight);
}
// 记录高度并累加
if (j == numColumns - 1) {
height += tempHeight;
rowHeightArrays[i] = tempHeight;
}
}
}
// 追加子项间分隔符占用的高度
height += (verticalSpacing * (lineNumber - 1));
int width = gridView.getMeasuredWidth();
// 创建位图
Bitmap bitmap = Bitmap.createBitmap(width, height, config);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(BACKGROUND_COLOR);
// 累加高度
int appendHeight = 0;
// 循环每一行绘制每个 Item Bitmap
for (int i = 0; i < lineNumber; i++) {
// 获取每一行最长列的高度
int itemHeight = rowHeightArrays[i];
// 循环列数
for (int j = 0; j < numColumns; j++) {
// 获取对应的索引
int position = i * numColumns + j;
// 小于总数才处理
if (position < itemCount) {
Bitmap bmp = bitmaps[position];
// 计算边距
int left = j * (horizontalSpacing + childWidth);
Matrix matrix = new Matrix();
matrix.postTranslate(left, appendHeight);
// 绘制到 Bitmap
canvas.drawBitmap(bmp, matrix, PAINT);
// 释放资源
BitmapUtils.recycle(bmp);
}
// 记录高度并累加
if (j == numColumns - 1) {
appendHeight += itemHeight + verticalSpacing;
}
}
}
return bitmap;
}
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "snapshotByGridView");
}
return null;
}
// ================
// = RecyclerView =
// ================
/**
* 通过 RecyclerView 绘制为 Bitmap
* @param recyclerView {@link RecyclerView}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByRecyclerView(final RecyclerView recyclerView) {
return snapshotByRecyclerView(recyclerView, BITMAP_CONFIG, 0, 0);
}
/**
* 通过 RecyclerView 绘制为 Bitmap
* @param recyclerView {@link RecyclerView}
* @param config {@link Bitmap.Config}
* @return {@link Bitmap}
*/
public static Bitmap snapshotByRecyclerView(
final RecyclerView recyclerView,
final Bitmap.Config config
) {
return snapshotByRecyclerView(recyclerView, config, 0, 0);
}
/**
* 通过 RecyclerView 绘制为 Bitmap
* @param recyclerView {@link RecyclerView}
* @param spacing 每列之间的间隔 |
* @return {@link Bitmap}
*/
public static Bitmap snapshotByRecyclerView(
final RecyclerView recyclerView,
final int spacing
) {
return snapshotByRecyclerView(recyclerView, BITMAP_CONFIG, spacing, spacing);
}
/**
* 通过 RecyclerView 绘制为 Bitmap
* @param recyclerView {@link RecyclerView}
* @param config {@link Bitmap.Config}
* @param spacing 每列之间的间隔 |
* @return {@link Bitmap}
*/
public static Bitmap snapshotByRecyclerView(
final RecyclerView recyclerView,
final Bitmap.Config config,
final int spacing
) {
return snapshotByRecyclerView(recyclerView, config, spacing, spacing);
}
/**
* 通过 RecyclerView 绘制为 Bitmap
* <pre>
* 不支持含 ItemDecoration 截图
* 如果数据太多推荐 copy 代码, 修改为保存每个 Item Bitmap 到本地, 并在绘制时获取绘制
* </pre>
* @param recyclerView {@link RecyclerView}
* @param config {@link Bitmap.Config}
* @param verticalSpacing 每行之间的间隔 -
* @param horizontalSpacing 每列之间的间隔 |
* @return {@link Bitmap}
*/
public static Bitmap snapshotByRecyclerView(
final RecyclerView recyclerView,
final Bitmap.Config config,
final int verticalSpacing,
final int horizontalSpacing
) {
if (recyclerView == null || config == null) return null;
try {
// 获取适配器
RecyclerView.Adapter adapter = recyclerView.getAdapter();
// 获取布局管理器
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager != null && adapter != null) {
// 判断布局类型
if (layoutManager instanceof GridLayoutManager) {
return snapshotByRecyclerView_GridLayoutManager(
recyclerView, config, verticalSpacing, horizontalSpacing
);
} else if (layoutManager instanceof LinearLayoutManager) {
return snapshotByRecyclerView_LinearLayoutManager(
recyclerView, config, verticalSpacing, horizontalSpacing
);
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
return snapshotByRecyclerView_StaggeredGridLayoutManager(
recyclerView, config, verticalSpacing, horizontalSpacing
);
}
throw new Exception(
String.format(
"Not Supported %s LayoutManager",
layoutManager.getClass().getSimpleName()
)
);
} else {
throw new Exception("Adapter or LayoutManager is null");
}
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "snapshotByRecyclerView");
}
return null;
}
// =================
// = LayoutManager =
// =================
/**
* 通过 RecyclerView GridLayoutManager 绘制为 Bitmap
* @param recyclerView {@link RecyclerView}
* @param config {@link Bitmap.Config}
* @param verticalSpacing 每行之间的间隔 -
* @param horizontalSpacing 每列之间的间隔 |
* @return {@link Bitmap}
*/
private static Bitmap snapshotByRecyclerView_GridLayoutManager(
final RecyclerView recyclerView,
final Bitmap.Config config,
final int verticalSpacing,
final int horizontalSpacing
) {
// 计算思路
// = 竖屏 =
// 每个 Item 宽度最大值固定为 (RecyclerView 宽度 - ( 列数 - 1) * 每列边距 ) / 列数
// 循环保存每行最大高度, 并累加每行之间的间隔, 用于 Bitmap 高度, 宽度用 RecyclerView 宽度
// = 横屏 =
// 循环保存每一行宽度以及每一行 ( 横着一行 ) 最大高度, 并且累加每行、每列之间的间隔
// 用于 Bitmap 高度, 宽度用 ( 每一行宽度累加值 ) 最大值
try {
// 获取适配器
RecyclerView.Adapter adapter = recyclerView.getAdapter();
// Item 总条数
int itemCount = adapter.getItemCount();
// 没数据则直接跳过
if (itemCount == 0) return null;
// 宽高
int width = 0, height = 0;
// View Bitmaps
Bitmap[] bitmaps = new Bitmap[itemCount];
// 获取布局管理器 ( 判断横竖布局 )