ImageUtils.java
64.9 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
package com.wd.common.imageglide;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.RequestFutureTarget;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.wd.fastcoding.base.R;
import com.wd.foundation.bean.custom.MenuBean;
import com.wd.foundation.wdkit.image.GlideApp;
import com.wd.foundation.wdkit.image.GlideOptions;
import com.wd.foundation.wdkit.utils.BitMapUtils;
import com.wd.foundation.wdkit.utils.ColorUtils;
import com.wd.foundation.wdkit.utils.DeviceUtil;
import com.wd.foundation.wdkit.utils.FileZipUtils;
import com.wd.foundation.wdkit.utils.SpUtils;
import com.wd.foundation.wdkit.utils.UiUtils;
import com.wd.foundation.wdkit.view.RenderViewOutlineProvider;
import com.wd.foundation.wdkitcore.tools.AppContext;
import com.wd.foundation.wdkitcore.tools.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.libpag.PAGFile;
import org.libpag.PAGView;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
/**
* @author yzm
* @date 2022/7/4
* @time 17:31.
*/
public class ImageUtils {
/**
* 圆角角度
*/
private static final int ANGLENUM = 6;
/**
* 主题背景图
*/
public static Bitmap defaultThemeBitmap = null;
private Map<String, SoftReference<Bitmap>> bitmapList = new HashMap<>();
/**
* 图片地址是否正确
*/
private static boolean isLoadSuccess = false;
private ImageUtils() {
}
private static class Holder {
private static final ImageUtils INSTANCE = new ImageUtils();
}
public static ImageUtils getInstance() {
return Holder.INSTANCE;
}
/**
* 优先加载的图片
*
* @param imageView
* @param url
*/
public void loadImageHighLev(ImageView imageView, String url) {
if (imageView == null) {
return;
}
try {
GlideApp.with(AppContext.getContext())
.load(url)
.priority(Priority.HIGH)
.apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.DATA))
.into(imageView);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 优先加载的图片
*/
public void loadImageHighLev(ImageView imageView, String url, @DrawableRes int defaultImg) {
if (imageView == null) {
return;
}
try {
GlideApp.with(imageView.getContext())
.load(url)
.priority(Priority.HIGH)
.apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.DATA))
.placeholder(defaultImg)
.error(defaultImg)
.into(imageView);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 优先加载的图片
*/
public void loadImageHighLev(ImageView imageView, String url, RequestListener<Drawable> listener) {
if (imageView == null || TextUtils.isEmpty(url)) {
return;
}
try {
GlideApp.with(imageView.getContext())
.load(url)
.priority(Priority.HIGH)
.apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.DATA))
.addListener(listener)
.into(imageView);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 优先加载的图片
*/
public void loadImageHighLev(ImageView imageView, File file, RequestListener<Drawable> listener) {
if (imageView == null || file == null) {
return;
}
try {
GlideApp.with(imageView.getContext())
.load(file)
.priority(Priority.HIGH)
.apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.DATA))
.addListener(listener)
.into(imageView);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 清理图片缓存
*/
public void clearCacheBitmapList() {
if (bitmapList.size() > 0) {
bitmapList.clear();
}
}
/**
* 首页背景图加载和保存 (其它地方慎用)
*
* @param imageView
* @param height
* @param url
*/
public void homeThemeImageLoadAndSave(ImageView imageView, int height, String url, int placeholder) {
if (imageView == null) {
return;
}
try {
if (!TextUtils.isEmpty(url) && url.contains(".gif")) {
GlideApp.with(AppContext.getContext()).load(url).apply(baseNoDefaultOptions()).into(imageView);
} else {
// 相同控件加载不同图片资源的默认图,用上一个url产生的bitmap作为默认图,无则为ull
defaultThemeBitmap = null;
if (!TextUtils.isEmpty(url)) {
Object object = imageView.getTag();
if (object != null) {
String cacheImageUrl = (String) object;
if (bitmapList.containsKey(cacheImageUrl)) {
SoftReference<Bitmap> imageBitmap = bitmapList.get(cacheImageUrl);
if (imageBitmap != null) {
defaultThemeBitmap = imageBitmap.get();
}
}
}
boolean flag = !avoidDoubleSameUrl(imageView, url);
if (flag) {
return;
}
if (!TextUtils.isEmpty(url) && bitmapList.containsKey(url)) {
SoftReference<Bitmap> imageBitmap = bitmapList.get(url);
if (imageBitmap != null) {
imageView.setImageBitmap(imageBitmap.get());
return;
}
}
} else {
imageView.setTag(null);
}
GlideApp.with(AppContext.getContext())
.asBitmap()
.load(url)
.apply(baseThemeBgScreenOptions(defaultThemeBitmap, placeholder))
.into(new BitmapImageViewTarget(imageView) {
@Override
protected void setResource(Bitmap bitmap) {
if (bitmap != null) {
Bitmap newBitmap = zoomCropBitmap(bitmap, 0, height, true);
if (newBitmap != null) {
imageView.setImageBitmap(newBitmap);
if (!bitmapList.containsKey(url)) {
// 把bitmap copy出新的bitmap 收集起来,因为glide 会把收集的bitmap内容修改掉
Bitmap copybitmap =
Bitmap.createBitmap(newBitmap.getWidth(), newBitmap.getHeight(), newBitmap.getConfig());
Canvas canvas = new Canvas(copybitmap);
Paint paint = new Paint();
canvas.drawBitmap(newBitmap, new Matrix(), paint);
SoftReference<Bitmap> softBitmap = new SoftReference<Bitmap>(newBitmap);
bitmapList.put(url, softBitmap);
}
}
}
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 使用Glide方式获取视频某一帧
* @param context 上下文
* @param uri 视频地址
* @param imageView 设置image
*/
public void loadVideoScreenshot(final Context context, String uri, ImageView imageView) {
Glide.with(context)
.setDefaultRequestOptions(
new RequestOptions()
.frame(0)
.centerCrop()
)
.load(uri)
.into(imageView);
}
/**
* 使用MediaMetadataRetriever方式获取视频某一帧
*/
public Bitmap createVideoThumbnail(String filePath, int kind) {
Bitmap bitmap = null;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
if (filePath.startsWith("http://")
|| filePath.startsWith("https://")
|| filePath.startsWith("widevine://")) {
retriever.setDataSource(filePath, new Hashtable<String, String>());
} else {
retriever.setDataSource(filePath);
}
bitmap = retriever.getFrameAtTime(0, MediaMetadataRetriever.OPTION_CLOSEST_SYNC); //retriever.getFrameAtTime(-1);
} catch (IllegalArgumentException ex) {
// Assume this is a corrupt video file
ex.printStackTrace();
} catch (RuntimeException ex) {
// Assume this is a corrupt video file.
ex.printStackTrace();
} finally {
try {
retriever.release();
} catch (RuntimeException ex) {
// Ignore failures while cleaning up.
ex.printStackTrace();
}
}
if (bitmap == null) {
return null;
}
if (kind == MediaStore.Images.Thumbnails.MINI_KIND) {//压缩图片 开始处
// Scale down the bitmap if it's too large.
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int max = Math.max(width, height);
if (max > 512) {
float scale = 512f / max;
int w = Math.round(scale * width);
int h = Math.round(scale * height);
bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
}//压缩图片 结束处
} else if (kind == MediaStore.Images.Thumbnails.MICRO_KIND) {
bitmap = ThumbnailUtils.extractThumbnail(bitmap,
96,
96,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
}
return bitmap;
}
/**
* 为ImageView加载图片
*
* @param imageView
* @param url
*/
public void loadImage(ImageView imageView, String url) {
if (imageView == null) {
return;
}
if (TextUtils.isEmpty(url)) {
imageView.setTag("");
GlideApp.with(AppContext.getContext())
.load(R.drawable.rmrb_placeholder_default)
.apply(baseOptions())
.into(imageView);
return;
}
if (!avoidDoubleSameUrl(imageView, url)) {
return;
}
try {
if (url.contains(".gif")) {
GlideApp.with(AppContext.getContext()).load(url).apply(baseGifOptions()).into(imageView);
} else {
GlideApp.with(AppContext.getContext()).load(url).apply(baseOptions()).into(imageView);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 为ImageView加载图片
*
* @param imageView
* @param url
* @param placeholderImg 携带制定默认图
*/
public void loadImage(ImageView imageView, String url, int placeholderImg) {
if (imageView == null) {
return;
}
if (TextUtils.isEmpty(url)) {
imageView.setTag("");
GlideApp.with(AppContext.getContext())
.load(placeholderImg)
.apply(baseOptions(placeholderImg, placeholderImg))
.into(imageView);
return;
}
if (!avoidDoubleSameUrl(imageView, url)) {
return;
}
try {
if (url.contains(".gif")) {
GlideApp.with(AppContext.getContext()).load(url).apply(baseGifOptions(placeholderImg, placeholderImg)).into(imageView);
} else {
GlideApp.with(AppContext.getContext()).load(url).apply(baseOptions(placeholderImg, placeholderImg)).into(imageView);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加载本地资源
*
* @param imageView
* @param url
*/
public void loadImage(ImageView imageView, int url) {
if (imageView == null) {
return;
}
try {
GlideApp.with(AppContext.getContext()).load(url).apply(baseGifOptions()).into(imageView);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加载本地资源
*
* @param imageView
* @param url
* @param placeholderImg
* @param errorImg
*/
public void loadImage(ImageView imageView, int url, int placeholderImg, int errorImg) {
if (imageView == null) {
return;
}
try {
GlideApp.with(AppContext.getContext()).load(url).apply(baseGifOptions(placeholderImg, errorImg)).into(imageView);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 为ImageView加载图片
*
* @param context
* @param url
*/
public void preloadImage(Context context, String url) {
preloadImage(context, url, new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target,
boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target,
DataSource dataSource, boolean isFirstResource) {
// Logger.e("预加载成功="+url);
return true;
}
});
}
/**
* 为ImageView加载图片
*
* @param context
* @param url
*/
public void preloadImage(Context context, String url, RequestListener<Drawable> requestListener) {
if (TextUtils.isEmpty(url) || context == null) {
return;
}
try {
// url = getOssUrl(context, url);
GlideApp.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.listener(requestListener)
.preload();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 为ImageView加载图片缓存到本地
*
* @param context
* @param url
*/
public void preloadImageInDir(Context context, String url) {
if (TextUtils.isEmpty(url) || context == null) {
return;
}
try {
FutureTarget<File> target =
GlideApp.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
// Logger.e("加载预览地址" + url);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加载圆角图片
*
* @param imageView
* @param url
* @param defaultImg
*/
public void loadAngleImage(ImageView imageView, String url, @DrawableRes int defaultImg) {
if (imageView == null) {
return;
}
if (TextUtils.isEmpty(url)) {
GlideApp.with(AppContext.getContext())
.load(defaultImg)
.apply(baseAngleOptions(defaultImg, imageView, ANGLENUM))
.into(imageView);
return;
}
// 防止刷新相同资源图片加载闪烁
if (!avoidDoubleSameUrl(imageView, url)) {
return;
}
try {
if (url.contains(".gif")) {
GlideApp.with(AppContext.getContext())
.load(url)
.apply(baseGifOptions(defaultImg, defaultImg, imageView, ANGLENUM))
.into(imageView);
} else {
GlideApp.with(AppContext.getContext())
.load(url)
.apply(baseAngleOptions(defaultImg, imageView, ANGLENUM))
.into(imageView);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void loadAngleImage(ImageView imageView, String url, Bitmap defaultImg, int angleInt) {
imageView.setOutlineProvider(new RenderViewOutlineProvider(UiUtils.dp2px(angleInt)));
imageView.setClipToOutline(true);
if (imageView == null) {
return;
}
if (TextUtils.isEmpty(url)) {
imageView.setTag("");
GlideApp.with(AppContext.getContext())
.load(defaultImg)
.apply(baseAngleOptions(defaultImg, imageView))
.into(imageView);
return;
}
// 防止刷新相同资源图片加载闪烁
if (!avoidDoubleSameUrl(imageView, url)) {
return;
}
try {
if (url.contains(".gif")) {
GlideApp.with(AppContext.getContext())
.load(url)
.apply(baseGifOptions(defaultImg, defaultImg, imageView))
.into(imageView);
} else {
GlideApp.with(AppContext.getContext())
.load(url)
.apply(baseAngleOptions(defaultImg, imageView))
.into(imageView);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void loadAngleImage(ImageView imageView, String url) {
loadAngleImage(imageView, url, R.drawable.rmrb_placeholder_default, ANGLENUM);
}
/**
* 加载原图到imageview
* 对控件本体进行圆角处理
*
* @param imageView
* @param url
*/
public void loadUrlCircularImageView(ImageView imageView, String url) {
loadUrlCircularImageView(imageView, url, R.drawable.rmrb_placeholder_default, ANGLENUM);
}
/**
* 加载原图到imageview
* 对控件本体进行圆角处理
*
* @param imageView
* @param url
* @param defaultImg
* @param angleInt
*/
public void loadUrlCircularImageView(ImageView imageView, String url, @DrawableRes int defaultImg, int angleInt) {
imageView.setOutlineProvider(new RenderViewOutlineProvider(UiUtils.dp2px(angleInt)));
imageView.setClipToOutline(true);
if (imageView == null) {
return;
}
if (TextUtils.isEmpty(url)) {
imageView.setTag("");
GlideApp.with(AppContext.getContext()).load(defaultImg).apply(baseOptions(defaultImg)).into(imageView);
return;
}
// 防止刷新相同资源图片加载闪烁
if (!avoidDoubleSameUrl(imageView, url)) {
return;
}
try {
if (url.contains(".gif")) {
GlideApp.with(AppContext.getContext())
.load(url)
.apply(baseOgifOptions(defaultImg, defaultImg))
.into(imageView);
} else {
GlideApp.with(AppContext.getContext())
.load(url)
.thumbnail(0.1f)
.apply(baseOptions(defaultImg))
.into(imageView);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 避免重复加载相同url
*
* @param imageView
* @param url
* @return true: 执行加载;false 停止继续加载图片
*/
private boolean avoidDoubleSameUrl(ImageView imageView, String url) {
if (TextUtils.isEmpty(url)) {
return true;
}
if (imageView != null) {
if (imageView.getTag() != null) {
String tagUrl = (String) imageView.getTag();
if (url.equals(tagUrl)) {
return false;
}
}
imageView.setTag(url);
}
return true;
}
/**
* 文件下载路径监听
*/
public void loadAngleImage(ImageView imageView, String url, @DrawableRes int defaultImg, int angle) {
if (imageView == null) {
return;
}
if (TextUtils.isEmpty(url)) {
imageView.setTag("");
GlideApp.with(AppContext.getContext())
.load(defaultImg)
.apply(baseAngleOptions(defaultImg, imageView, angle))
.into(imageView);
return;
}
if (!avoidDoubleSameUrl(imageView, url)) {
return;
}
try {
if (url.contains(".gif")) {
GlideApp.with(AppContext.getContext())
.load(url)
.apply(baseGifOptions(defaultImg, defaultImg, imageView, angle))
.into(imageView);
} else {
GlideApp.with(AppContext.getContext())
.load(url)
.apply(baseAngleOptions(defaultImg, imageView, angle))
.into(imageView);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加载圆形图片
*
* @param imageView
* @param url
* @placeholderImg 默认、错误图片
*/
public void loadImageCircle(final ImageView imageView, String url, @DrawableRes int defaultImg,RequestListener listener) {
if (imageView == null) {
return;
}
if (TextUtils.isEmpty(url)) {
imageView.setTag("");
GlideApp.with(AppContext.getContext())
.load(defaultImg)
.listener(listener)
.apply(baseCicleOptions(defaultImg, defaultImg))
.into(imageView);
return;
}
if (!avoidDoubleSameUrl(imageView, url)) {
return;
}
try {
GlideApp.with(AppContext.getContext())
.load(url)
.listener(listener)
.apply(baseCicleOptions(defaultImg, defaultImg))
.into(imageView);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加载圆形图片
*
* @param imageView
* @param url
* @placeholderImg 默认、错误图片
*/
public void loadImageCircle(final ImageView imageView, String url, @DrawableRes int defaultImg) {
if (imageView == null) {
return;
}
if (TextUtils.isEmpty(url)) {
imageView.setTag("");
GlideApp.with(AppContext.getContext())
.load(defaultImg)
.apply(baseCicleOptions(defaultImg, defaultImg))
.into(imageView);
return;
}
if (!avoidDoubleSameUrl(imageView, url)) {
return;
}
try {
GlideApp.with(AppContext.getContext())
.load(url)
.apply(baseCicleOptions(defaultImg, defaultImg))
.into(imageView);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加载圆形图片
*
* @param imageView
* @param url
* @placeholderImg 默认、错误图片
*/
public void loadImageCrop(final ImageView imageView, String url) {
if (imageView == null) {
return;
}
if (TextUtils.isEmpty(url)) {
imageView.setTag("");
GlideApp.with(AppContext.getContext())
.load(R.drawable.rmrb_placeholder_default)
.apply(baseOptions().centerCrop())
.into(imageView);
return;
}
if (!avoidDoubleSameUrl(imageView, url)) {
return;
}
try {
if (url.contains(".gif")) {
GlideApp.with(AppContext.getContext()).load(url).apply(baseGifOptions().centerCrop()).into(imageView);
} else {
GlideApp.with(AppContext.getContext()).load(url).apply(baseOptions().centerCrop()).into(imageView);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* error图片和默认图片不一样
*
* @param imageView
* @param url
* @param defaultImg 默认图片
* @param errorImg 错误图片
*/
public void loadImageDifferent(ImageView imageView, String url, @DrawableRes int defaultImg, int errorImg) {
if (imageView == null) {
return;
}
if (TextUtils.isEmpty(url)) {
imageView.setTag("");
GlideApp.with(AppContext.getContext())
.load(defaultImg)
.transition(new DrawableTransitionOptions().crossFade())
.apply(baseOptions(defaultImg, errorImg))
.into(imageView);
return;
}
if (!avoidDoubleSameUrl(imageView, url)) {
return;
}
try {
if (url.contains(".gif")) {
GlideApp.with(AppContext.getContext())
.load(url)
.transition(new DrawableTransitionOptions().crossFade())
.apply(baseGifOptions(defaultImg, errorImg, imageView, ANGLENUM))
.into(imageView);
} else {
GlideApp.with(AppContext.getContext())
.load(url)
.transition(new DrawableTransitionOptions().crossFade())
.apply(baseOptions(defaultImg, errorImg))
.into(imageView);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加载图片有回调
*
* @param imageView
* @param url
* @param defaultImg 默认图
* @param errorImg 加载异常占位图
* @param listener 加载监听
*/
public void loadImageHaveListener(Context context, ImageView imageView, String url, @DrawableRes int defaultImg, int errorImg, RequestListener listener) {
if (imageView == null) {
return;
}
if (TextUtils.isEmpty(url)) {
imageView.setTag("");
GlideApp.with(AppContext.getContext())
.load(defaultImg)
.listener(listener)
.transition(new DrawableTransitionOptions().crossFade())
.apply(baseOptions(defaultImg, errorImg))
.into(imageView);
return;
}
if (!avoidDoubleSameUrl(imageView, url)) {
return;
}
try {
if (url.contains(".gif")) {
GlideApp.with(AppContext.getContext())
.load(url)
.listener(listener)
.thumbnail(Glide.with(context).load(defaultImg))
.transition(new DrawableTransitionOptions().crossFade())
.apply(baseGifOptions(defaultImg, errorImg, imageView, ANGLENUM))
.into(imageView);
} else {
GlideApp.with(AppContext.getContext())
.load(url)
.listener(listener)
.thumbnail(Glide.with(context).load(defaultImg))
.transition(new DrawableTransitionOptions().crossFade())
.apply(baseOptions(defaultImg, errorImg))
.into(imageView);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static RequestOptions baseOptions(@DrawableRes int placeholderImg, @DrawableRes int errorImg) {
return new RequestOptions().placeholder(placeholderImg)
.error(errorImg)
.diskCacheStrategy(DiskCacheStrategy.DATA);
}
private static RequestOptions baseOptions() {
return new RequestOptions().diskCacheStrategy(DiskCacheStrategy.DATA)
.placeholder(R.drawable.rmrb_placeholder_default)
.error(R.drawable.rmrb_placeholder_default)
.dontAnimate();
}
/**
* 设置默认主题图
*
* @param defaultBitmap
* @return
*/
private RequestOptions baseThemeBgScreenOptions(Bitmap defaultBitmap, int placeholder) {
RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.DATA)
.dontAnimate();
if (defaultBitmap != null) {
BitmapDrawable drawable = new BitmapDrawable(AppContext.getContext().getResources(), defaultBitmap);
options.placeholder(drawable);
} else {
options.placeholder(placeholder);
}
return options;
}
private static RequestOptions baseCicleOptions(@DrawableRes int placeholderImg, @DrawableRes int errorImg) {
return new RequestOptions().placeholder(placeholderImg)
.error(errorImg)
.circleCrop()
.diskCacheStrategy(DiskCacheStrategy.DATA);
}
private static RequestOptions baseOptions(@DrawableRes int placeholderImg) {
return new RequestOptions().centerCrop()
.placeholder(placeholderImg)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE);
}
private static RequestOptions baseOgifOptions(@DrawableRes int placeholderImg, @DrawableRes int errorImg) {
return new RequestOptions().placeholder(placeholderImg)
.error(errorImg)
.diskCacheStrategy(DiskCacheStrategy.DATA);
}
/**
* 圆角图片
*
* @param imageView
* @param angle
* @return
*/
private static RequestOptions baseAngleOptions(@DrawableRes int placeholderImg, ImageView imageView, int angle) {
return new RequestOptions().centerCrop()
.transform(new GlideRoundTransform(AppContext.getContext(), angle))
.placeholder(placeholderImg)
// .skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.DATA);
}
private static RequestOptions baseAngleOptions(Bitmap placeholderImg, ImageView imageView) {
BitmapDrawable drawable = new BitmapDrawable(AppContext.getContext().getResources(), placeholderImg);
return new RequestOptions().centerCrop()
.placeholder(drawable)
// .skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.DATA);
}
// GIF
private static RequestOptions baseGifOptions(@DrawableRes int placeholderImg, @DrawableRes int errorImg,
ImageView imageView, int angle) {
return new RequestOptions().placeholder(placeholderImg)
.error(errorImg)
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.transform(new GlideRoundTransform(AppContext.getContext(), angle));
}
private static RequestOptions baseGifOptions(Bitmap placeholderImg, Bitmap errorImg, ImageView imageView) {
BitmapDrawable drawable = new BitmapDrawable(AppContext.getContext().getResources(), placeholderImg);
return new RequestOptions().placeholder(drawable)
.error(drawable)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.skipMemoryCache(true);
}
// GIF
private static RequestOptions baseGifOptions(@DrawableRes int placeholderImg, @DrawableRes int errorImg) {
return new RequestOptions().placeholder(placeholderImg)
.error(errorImg)
// .skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.DATA);
}
// GIF
private static RequestOptions baseGifOptions() {
return new RequestOptions().diskCacheStrategy(DiskCacheStrategy.DATA)
// .skipMemoryCache(true)
.placeholder(R.drawable.rmrb_placeholder_default)
.error(R.drawable.rmrb_placeholder_default);
}
// 不适用占位图和错误图
private static RequestOptions baseNoDefaultOptions() {
return new RequestOptions().diskCacheStrategy(DiskCacheStrategy.DATA);
}
/**
* 文件下载路径监听
*/
public interface OnImageResultListener {
void onResult(String localPath);
}
// 加载本地圆角图片
public void loadAngleImage(ImageView imageView, File url, @DrawableRes int defaultImg) {
if (imageView == null) {
return;
}
if (TextUtils.isEmpty(url.getAbsolutePath())) {
imageView.setTag("");
GlideApp.with(AppContext.getContext())
.load(url)
.apply(baseAngleOptions(defaultImg, imageView, ANGLENUM))
.into(imageView);
return;
}
// 防止刷新相同资源图片加载闪烁
if (!avoidDoubleSameUrl(imageView, url.getAbsolutePath())) {
return;
}
try {
if (url.getAbsolutePath().contains(".gif")) {
GlideApp.with(AppContext.getContext())
.load(url)
.apply(baseGifOptions(defaultImg, defaultImg, imageView, ANGLENUM))
.into(imageView);
} else {
GlideApp.with(AppContext.getContext())
.load(url)
.apply(baseAngleOptions(defaultImg, imageView, ANGLENUM))
.into(imageView);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 该方法调用时要放在子线程中
*
* @param imgUrl 网络资源图片路径
* @return Bitmap
*/
public static Bitmap netToLocalBitmap(String imgUrl) {
Bitmap bitmap;
try {
// 对资源链接
URL url = new URL(imgUrl);
// 打开输入流
InputStream inputStream = url.openStream();
// 对网上资源进行下载转换位图图片
bitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
return bitmap;
} catch (IOException e) {
return null;
}
}
/**
* 设置底部导航tab 中文字颜色
*
* @param textView
* @param tabData
* @param selectedFlag
* @param isImmersePageFlag 沉浸式
*/
public void setBottomTabTextColor(TextView textView, MenuBean tabData, boolean selectedFlag, boolean isImmersePageFlag) {
//设置文字颜色
if (selectedFlag) {
if (isImmersePageFlag) {
ColorUtils.setTextColor(textView, StringUtils.isBlank(tabData.getImmersiveNameCColor()) ? "#CB0000" : tabData.getImmersiveNameCColor());
} else {
//夜间模式
if (SpUtils.isNightMode()) {
ColorUtils.setTextColor(textView, StringUtils.isBlank(tabData.getNightNameCColor()) ? "#B71D26" : tabData.getNightNameCColor());
}
//日间模式
else {
ColorUtils.setTextColor(textView, StringUtils.isBlank(tabData.getNameCColor()) ? "#CB0000" : tabData.getNameCColor());
}
}
} else {
if (isImmersePageFlag) {
ColorUtils.setTextColor(textView, StringUtils.isBlank(tabData.getImmersiveNameColor()) ? "#999999" : tabData.getImmersiveNameColor());
} else {
//夜间模式
if (SpUtils.isNightMode()) {
ColorUtils.setTextColor(textView, StringUtils.isBlank(tabData.getNightNameColor()) ? "#A3A3A3" : tabData.getNightNameColor());
}
//日间模式
else {
ColorUtils.setTextColor(textView, StringUtils.isBlank(tabData.getNameColor()) ? "#888888" : tabData.getNameColor());
}
}
}
}
/**
* 加载选中的底部图片方法
*
* @param imageView
* @param tabData
* @param selectedFlag true: 选中;false:未选中
* @param isImmersePageFlag 沉浸式
*/
public void loadBottomTabImage(ImageView imageView, MenuBean tabData, boolean selectedFlag, boolean isImmersePageFlag) {
if (imageView == null) {
return;
}
// 非沉浸式图标 url
String url = null;
// 非沉浸式图标 兜底icon
int localIcon;
//沉浸式图标 url
String immerseUrl;
//沉浸式图标 兜底icon
int localImmerseIcon;
if (selectedFlag) {
if (SpUtils.isNightMode()) {
//夜间
url = tabData.getNightIconCUrl();
} else {
//日间
url = tabData.getSelIcon();
}
localIcon = tabData.getLocalIconC();
// 沉浸式 tab icon用准备 icon兜底,没有则用非沉浸式icon兜底
localImmerseIcon = tabData.getLocalImmerseIconC() == 0 ? tabData.getLocalIconC() : tabData.getLocalImmerseIconC();
immerseUrl = tabData.getImmersiveIconCUrl();
} else {
if (SpUtils.isNightMode()) {
//夜间
url = tabData.getNightIconUrl();
} else {
//日间
url = tabData.getNormalIcon();
}
localIcon = tabData.getLocalIcon();
// 沉浸式未选中tab icon用非沉浸式未选中tab icon 兜底
localImmerseIcon = tabData.getLocalIcon();
immerseUrl = tabData.getImmersiveIconUrl();
}
// 是否是沉浸式页面
if (isImmersePageFlag) {
if (url != null && url.contains(".gif")) {
if ("default.gif".equals(url)) {
loadOneTimeGif(imageView, "", localImmerseIcon);
} else {
loadOneTimeGif(imageView, immerseUrl, localImmerseIcon);
}
} else {
loadImageHighLev(imageView, immerseUrl, localImmerseIcon);
}
} else {
if (url != null && url.contains(".gif")) {
if ("default.gif".equals(url)) {
loadOneTimeGif(imageView, "", localIcon);
} else {
loadOneTimeGif(imageView, url, localIcon);
}
} else {
loadImageHighLev(imageView, url, localIcon);
}
}
}
/**
* 加载选中的底部图片方法
*
* @param imageView
* @param tabData
* @param selectedFlag true: 选中;false:未选中
* @param isImmersePageFlag 沉浸式
*/
public void loadBottomTabImage(PAGView pageView, ImageView imageView, MenuBean tabData,
boolean selectedFlag, boolean isImmersePageFlag) {
if (imageView == null) {
return;
}
// 非沉浸式图标 url
String url = null;
// 非沉浸式图标 兜底icon
int localIcon;
String localPag = tabData.getLocalPag();
//沉浸式图标 url
String immerseUrl;
//沉浸式图标 兜底icon
int localImmerseIcon;
if (selectedFlag) {
if (SpUtils.isNightMode()) {
//夜间
url = tabData.getNightIconCUrl();
} else {
//日间
url = tabData.getSelIcon();
}
localIcon = tabData.getLocalIconC();
// 沉浸式 tab icon用准备 icon兜底,没有则用非沉浸式icon兜底
localImmerseIcon = tabData.getLocalImmerseIconC() == 0 ? tabData.getLocalIconC() : tabData.getLocalImmerseIconC();
immerseUrl = tabData.getImmersiveIconCUrl();
if (isImmersePageFlag) {
localPag = tabData.getLocalImmPage();
} else {
localPag = tabData.getLocalPag();
}
} else {
if (SpUtils.isNightMode()) {
//夜间
url = tabData.getNightIconUrl();
} else {
//日间
url = tabData.getNormalIcon();
}
localIcon = tabData.getLocalIcon();
// 沉浸式未选中tab icon用非沉浸式未选中tab icon 兜底
localImmerseIcon = tabData.getLocalIcon();
immerseUrl = tabData.getImmersiveIconUrl();
}
// 是否是沉浸式页面
if (isImmersePageFlag) {
// 沉浸式
loadTabImage(pageView, imageView, immerseUrl, localImmerseIcon, localPag, selectedFlag);
} else {
// 非沉浸式
loadTabImage(pageView, imageView, url, localIcon, localPag, selectedFlag);
}
}
/**
* 加载tab 图片、gif、pag资源
*
* @param pageView
* @param imageView
* @param url
* @param localIcon
* @param localPag
*/
private void loadTabImage(PAGView pageView, ImageView imageView, String url, int localIcon, String localPag, boolean selectedFlag) {
if (!TextUtils.isEmpty(url)) {
if (url != null && url.contains(".gif")) {
imageView.setVisibility(View.VISIBLE);
if (pageView != null) {
pageView.setVisibility(View.INVISIBLE);
pageView.setAlpha(0);
}
if ("default.gif".equals(url)) {
loadOneTimeGif(imageView, "", localIcon);
} else {
loadOneTimeGif(imageView, url, localIcon);
}
} else if (url != null && url.contains(".pag")) {
imageView.setVisibility(View.INVISIBLE);
if (pageView != null) {
pageView.setVisibility(View.VISIBLE);
pageView.setAlpha(1);
PAGFile.LoadAsync(url, new PAGFile.LoadListener() {
@Override
public void onLoad(PAGFile pagFile) {
if (pagFile == null) {
pagFile = PAGFile.Load(pageView.getContext().getAssets(), localPag);
}
pageView.setComposition(pagFile);
pageView.setRepeatCount(1);
pageView.play();
}
});
}
} else {
imageView.setVisibility(View.VISIBLE);
if (pageView != null) {
pageView.setVisibility(View.INVISIBLE);
pageView.setAlpha(0);
}
loadImageHighLev(imageView, url, localIcon);
}
} else {
// if (selectedFlag) {
// imageView.setVisibility(View.INVISIBLE);
// if (pageView != null) {
// pageView.setVisibility(View.VISIBLE);
// pageView.setAlpha(1);
//
// PAGFile pagFile = PAGFile.Load(pageView.getContext().getAssets(), localPag);
// pageView.setComposition(pagFile);
// pageView.setRepeatCount(1);
// pageView.play();
// }
// } else {
imageView.setVisibility(View.VISIBLE);
if (pageView != null) {
pageView.setVisibility(View.INVISIBLE);
pageView.setAlpha(0);
}
loadImageHighLev(imageView, url, localIcon);
// }
}
}
/**
* 加载图片资源和gif资源(gif资源只播放一次)
*
* @param imageView
* @param url
*/
public void loadChannelImageUrl(ImageView imageView, String url) {
if (url != null && url.contains(".gif")) {
if (url.contains("?")) {
url = url.substring(0, url.indexOf("?"));
}
loadOneTimeGif(imageView, url, 0);
} else {
GlideApp.with(imageView.getContext())
.load(url)
.priority(Priority.HIGH)
.apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.DATA))
.into(imageView);
}
}
/**
* 加载一次gif
*/
public void loadOneTimeGif(ImageView imageView, String url, @DrawableRes int defaultImg) {
loadOneTimeGif(url, imageView, null, defaultImg);
}
/**
* 加载一次gif
*/
public void loadOneTimeGif(String url, final ImageView imageView, final GifListener gifListener,
@DrawableRes int defaultImg) {
RequestOptions requestOptions = new RequestOptions().skipMemoryCache(true);
try {
RequestBuilder<GifDrawable> gifDrawableRequestBuilder = GlideApp.with(AppContext.getContext()).asGif();
if (!TextUtils.isEmpty(url)) {
gifDrawableRequestBuilder.load(url);
} else {
gifDrawableRequestBuilder.load(defaultImg);
}
gifDrawableRequestBuilder.apply(requestOptions).listener(new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target,
boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target,
DataSource dataSource, boolean isFirstResource) {
try {
Field gifStateField = GifDrawable.class.getDeclaredField("state");
gifStateField.setAccessible(true);
Class gifStateClass =
Class.forName("com.bumptech.glide.load.resource.gif.GifDrawable$GifState");
Field gifFrameLoaderField = gifStateClass.getDeclaredField("frameLoader");
gifFrameLoaderField.setAccessible(true);
Class gifFrameLoaderClass =
Class.forName("com.bumptech.glide.load.resource.gif.GifFrameLoader");
Field gifDecoderField = gifFrameLoaderClass.getDeclaredField("gifDecoder");
gifDecoderField.setAccessible(true);
Class gifDecoderClass = Class.forName("com.bumptech.glide.gifdecoder.GifDecoder");
Object gifDecoder = gifDecoderField.get(gifFrameLoaderField.get(gifStateField.get(resource)));
Method getDelayMethod = gifDecoderClass.getDeclaredMethod("getDelay", int.class);
getDelayMethod.setAccessible(true);
// 设置只播放一次
resource.setLoopCount(1);
if (null != gifListener) {
// 获得总帧数
int count = resource.getFrameCount();
int delay = 0;
for (int i = 0; i < count; i++) {
// 计算每一帧所需要的时间进行累加
delay += (int) getDelayMethod.invoke(gifDecoder, i);
}
imageView.postDelayed(new Runnable() {
@Override
public void run() {
if (gifListener != null) {
gifListener.gifPlayComplete();
}
}
}, delay);
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}).into(imageView);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Gif播放完毕回调
*/
public interface GifListener {
void gifPlayComplete();
}
/**
* 处理图片
*
* @param bitmap 所要转换的bitmap
* @param dst_w 新的宽
* @param dst_h 新的高
* @return 指定宽高的bitmap
*/
public Bitmap zoomImg(Bitmap bitmap, int dst_w, int dst_h) {
if (bitmap == null) {
return null;
}
int src_w = bitmap.getWidth();
int src_h = bitmap.getHeight();
float scale_w = ((float) dst_w) / src_w;
float scale_h = ((float) dst_h) / src_h;
Matrix matrix = new Matrix();
matrix.postScale(scale_w, scale_h);
Bitmap dstbmp = Bitmap.createBitmap(bitmap, 0, 0, src_w, src_h, matrix, true);
return dstbmp;
}
/**
* 分享图片裁剪
*
* @param bitmap
* @return
*/
public Bitmap getShareZoomImg(Bitmap bitmap, int dst_w, int dst_h) {
if (bitmap == null) {
return null;
}
int x = 0;
int y = 0;
int outputSize = 150;
int src_w = bitmap.getWidth();
int src_h = bitmap.getHeight();
if (src_w > src_h) {
x = (src_w - src_h) / 2;
outputSize = src_h;
} else if (src_w < src_h) {
y = (src_h - src_w) / 2;
outputSize = src_w;
} else {
outputSize = src_w;
}
Bitmap tempBitmap = Bitmap.createBitmap(bitmap, x, y, outputSize, outputSize);
return zoomImg(tempBitmap, dst_w, dst_h);
}
/**
* 文字绘制到图片上
*
* @param text 文字内容
* @param imgResourceId 目标图
* @param mContext
* @return
*/
public static Bitmap drawText2Bitmap(String text, int imgResourceId, Context mContext) {
try {
Resources resources = mContext.getResources();
float scale = resources.getDisplayMetrics().density;
Bitmap bitmap = BitmapFactory.decodeResource(resources, imgResourceId);
Bitmap.Config bitmapConfig = bitmap.getConfig();
// set default bitmap config if none
if (bitmapConfig == null) {
bitmapConfig = Bitmap.Config.ARGB_8888;
}
// resource bitmaps are imutable, so we need to convert it to mutable one
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.FAKE_BOLD_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); // new antialised Paint
paint.setColor(Color.parseColor("#FF6C0A")); // text color - #3D3D3D
paint.setTextSize((int) (22 * scale)); // text size in pixels
// paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY); // text shadow
// draw text to the Canvas center
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
int x = (bitmap.getWidth() - bounds.width()) / 6;
int y = (bitmap.getHeight() + bounds.height()) / 5;
int distanceY = bounds.height() / 2;
canvas.drawText(text, x * scale, y * scale - distanceY, paint);
return bitmap;
} catch (Exception e) {
return null;
}
}
/**
* 添加背景
*
* @param color
* @param orginBitmap
* @return
*/
public Bitmap setBitmapBg(int color, Bitmap orginBitmap, int THUMB_SIZE) {
Paint paint = new Paint();
paint.setColor(color);
// Bitmap bitmap = Bitmap.createBitmap(orginBitmap.getWidth(),
// orginBitmap.getHeight(), orginBitmap.getConfig());
Bitmap bitmap = zoomImg(orginBitmap, THUMB_SIZE, THUMB_SIZE);
Canvas canvas = new Canvas(bitmap);
canvas.drawRect(0, 0, THUMB_SIZE, THUMB_SIZE, paint);
canvas.drawBitmap(orginBitmap, 0, 0, paint);
return bitmap;
}
/**
* 合成图片
*
* @param background
* @param foreground
* @return
*/
public Bitmap toConformBitmap(Bitmap background, Bitmap foreground) {
if (background == null) {
return null;
}
int bgWidth = background.getWidth();
int bgHeight = background.getHeight();
// create the new blank bitmap 创建一个新的和SRC长度宽度一样的位图
Bitmap newbmp = Bitmap.createBitmap(bgWidth, bgHeight, Bitmap.Config.ARGB_8888);
Canvas cv = new Canvas(newbmp);
// draw bg into
cv.drawBitmap(background, 0, 0, null);// 在 0,0坐标开始画入bg
// draw fg into
cv.drawBitmap(foreground, 0, 0, null);// 在 0,0坐标开始画入fg ,可以从任意位置画入
// save all clip
cv.save();// 保存
// store
cv.restore();// 存储
return newbmp;
}
/**
* 图片按比例大小压缩方法
*
* @param image (根据Bitmap图片压缩)
* @param outputSize 输出的文件大小(KB)
* @return
*/
public static Bitmap compressScale(Bitmap image, long outputSize) {
return FileZipUtils.compressScale(image, outputSize);
}
/**
* 把bitmap,依据needWith按bitmap 宽高比例计算出新高度,裁剪出符合 needWith和needHeight的bitmap
*
* @param bitmap 原
* @param needWith needWith= 0 ,按屏幕宽度,
* @param needHeight needHeight = 0 ,按屏幕高度
* @param cropFlag true: 按照needHeight 需要的高度裁剪,从顶部裁剪;flase:不需按照needHeight裁剪
* @return
*/
private Bitmap zoomCropBitmap(Bitmap bitmap, int needWith, int needHeight, boolean cropFlag) {
if (bitmap == null) {
return null;
}
int bitmapW = bitmap.getWidth();
int bitmapH = bitmap.getHeight();
int standarWith = needWith;
if (needWith == 0) {
standarWith = DeviceUtil.getDeviceWidth();
}
int standarHeight = needHeight;
if (needHeight == 0) {
standarHeight = DeviceUtil.getScreenHeight();
}
// 1、 以屏幕宽度为准,按图片实际比例缩放产生新的bitmap;
int bigBitmapH = (int) (bitmapH * standarWith / bitmapW);
int bigBitmapW = standarWith;
Bitmap zoomBitmap = BitMapUtils.zoomBitMap(bitmap, bigBitmapW, bigBitmapH);
if (cropFlag) {
if (bitmapW > bitmapH) {
// 横图
} else {
// 竖图或正方形
// 2、缩放产生新的bitmap的高度小于或等于手机屏幕高度,使用缩放产生新的bitmap;反之 则按照手机屏幕的高度 裁剪放大的bitmap
if (zoomBitmap.getHeight() <= standarHeight) {
} else {
//裁剪图片
Bitmap scaledSmallBitamp = Bitmap.createBitmap(zoomBitmap, 0, 0, zoomBitmap.getWidth(), standarHeight);
zoomBitmap = scaledSmallBitamp;
}
}
}
return zoomBitmap;
}
/**
* Glide 加载动图,增加圆角,亲测有效
*
* @param mContext 上下文
* @param imgView 加载对象
* @param url 图片链接
* @param listener 回调
* @param fillet 角度大小
*/
public static void loadGlideShape(final Context mContext, final ImageView imgView, String url, final int fillet, RequestListener listener) {
if (mContext == null) {
return;
}
if (!TextUtils.isEmpty(url)) {
// 设置图片圆角角度
RequestOptions gifOptions =
RequestOptions.bitmapTransform(new RoundedCorners(fillet))
.diskCacheStrategy(DiskCacheStrategy.DATA);
try {
GlideApp.with(mContext).load(url).listener(listener).apply(gifOptions).into(imgView);
} catch (Exception e) {
}
}
}
/**
* 加载图片资源
*
* @param imageView
* @param url
* @param callback 成功获取图片资源
*/
public void loadImageSource(ImageView imageView, String url, LoadImageCallback callback) {
if (imageView == null) {
return;
}
try {
if (!TextUtils.isEmpty(url) && url.contains(".gif")) {
GlideApp.with(AppContext.getContext()).load(url).apply(baseGifOptions()).into(imageView);
} else {
GlideApp.with(AppContext.getContext())
.asBitmap()
.load(url)
.apply(baseOptions())
.into(new BitmapImageViewTarget(imageView) {
@Override
protected void setResource(Bitmap bitmap) {
if (bitmap != null) {
if (callback != null) {
callback.callbackBitmap(bitmap);
}
imageView.setImageBitmap(bitmap);
}
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static File getCache(Context context, String url) {
File cacheFile = null;
RequestFutureTarget<File> futureTarget = (RequestFutureTarget<File>) GlideApp.with(context).downloadOnly().load(url).apply(new GlideOptions().onlyRetrieveFromCache(true)).submit();
Class<?> class1 = futureTarget.getClass();
Field field = null;
try {
//等待Glide给resource对象赋值
synchronized (futureTarget) {
futureTarget.wait();
}
field = class1.getDeclaredField("resource");
field.setAccessible(true);//开放权限
cacheFile = (File) field.get(futureTarget);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return cacheFile;
}
/**
* 验证可用的url,此方法有点耗时
*/
public static boolean checkLoadUrl(Context context, String url) {
GlideApp.with(context)
.load(url)
.into(new CustomTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull @NotNull Drawable resource, @Nullable @org.jetbrains.annotations.Nullable Transition<? super Drawable> transition) {
isLoadSuccess = true;
}
@Override
public void onLoadCleared(@Nullable @org.jetbrains.annotations.Nullable Drawable placeholder) {
}
@Override
public void onLoadFailed(@Nullable @org.jetbrains.annotations.Nullable Drawable errorDrawable) {
isLoadSuccess = false;
}
});
return isLoadSuccess;
}
/**
* 加载图片资源依据网络信息和设置开关
*
* @param imageView
* @param url
* @param placeholderImg
*/
public void loadImageSourceByNetStatus(ImageView imageView, String url, int placeholderImg) {
//仅wifi开关
String imgTag = SpUtils.getWifiLoadImgSwitchTag();
boolean isWifi = isWifi();
if (!isWifi && StringUtils.isEqual("1", imgTag)) {
// 不是WiFi网络环境并且设置仅WiFi网络加载图片
GlideApp.with(AppContext.getContext())
.load(placeholderImg)
//淡入淡出的动画效果
.transition(new DrawableTransitionOptions().crossFade())
.apply(baseOptions(placeholderImg, placeholderImg))
.into(imageView);
} else {
loadImage(imageView, url, placeholderImg);
}
}
/**
* 判断是否是wifi连接
*
* @return
*/
private boolean isWifi() {
ConnectivityManager mConnectivityManager =
(ConnectivityManager) AppContext.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (mConnectivityManager == null) {
return false;
}
NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mWiFiNetworkInfo != null) {
return mWiFiNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI;
}
return false;
}
/**
* 获取图片bitmap
*/
public static void loadBitmapListener(Context mContext, String url, CustomTarget<Bitmap> listener) {
if (mContext == null) {
return;
}
RequestOptions mRequestOptions = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.DATA)
.dontAnimate();
GlideApp.with(mContext).asBitmap().load(url).apply(mRequestOptions).into(listener);
}
/**
* 给viewGroup 相关的控件加载图片
*
* @param mContext
* @param url
* @param viewGroup
* @param placeHolderId
*/
public static void loadBitmapListener(Context mContext, String url, ViewGroup viewGroup, int placeHolderId) {
if (mContext == null) {
return;
}
RequestOptions mRequestOptions = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.DATA)
.dontAnimate();
GlideApp.with(mContext).asBitmap().load(url).apply(mRequestOptions).into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull @NotNull Bitmap resource, @Nullable @org.jetbrains.annotations.Nullable Transition<? super Bitmap> transition) {
if (resource != null) {
viewGroup.setBackground(new BitmapDrawable(viewGroup.getContext().getResources(), resource));
} else {
viewGroup.setBackgroundResource(placeHolderId);
}
}
@Override
public void onLoadCleared(@Nullable @org.jetbrains.annotations.Nullable Drawable placeholder) {
viewGroup.setBackgroundResource(placeHolderId);
}
});
}
/**
* 获取图片bitmap
*/
public static void loadBitmapListener(Context mContext, Uri uri, CustomTarget<Bitmap> listener) {
if (mContext == null) {
return;
}
RequestOptions mRequestOptions = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.DATA)
.dontAnimate();
GlideApp.with(mContext).asBitmap().load(uri).apply(mRequestOptions).into(listener);
}
}