5
5
import android .content .res .Resources ;
6
6
import android .graphics .Bitmap ;
7
7
import android .graphics .Bitmap .CompressFormat ;
8
- import android .graphics .Bitmap .Config ;
9
8
import android .graphics .BitmapFactory ;
10
- import android .graphics .BitmapShader ;
11
9
import android .graphics .Canvas ;
12
10
import android .graphics .Color ;
13
11
import android .graphics .ColorMatrix ;
21
19
import android .graphics .Rect ;
22
20
import android .graphics .RectF ;
23
21
import android .graphics .Shader ;
24
- import android .graphics .drawable .BitmapDrawable ;
25
22
import android .graphics .drawable .Drawable ;
26
- import android .hardware .camera2 .params .BlackLevelPattern ;
27
23
import android .media .ExifInterface ;
28
24
import android .os .Build ;
29
25
import android .renderscript .Allocation ;
30
26
import android .renderscript .Element ;
31
27
import android .renderscript .RenderScript ;
32
28
import android .renderscript .ScriptIntrinsicBlur ;
29
+ import android .view .View ;
33
30
34
31
import java .io .BufferedInputStream ;
35
32
import java .io .BufferedOutputStream ;
36
- import java .io .ByteArrayInputStream ;
37
33
import java .io .ByteArrayOutputStream ;
38
34
import java .io .File ;
39
35
import java .io .FileDescriptor ;
43
39
import java .io .IOException ;
44
40
import java .io .InputStream ;
45
41
import java .io .OutputStream ;
46
- import java .util .Date ;
47
42
48
43
/**
49
44
* <pre>
@@ -74,7 +69,7 @@ public static byte[] bitmap2Bytes(Bitmap bitmap, CompressFormat format) {
74
69
* byteArr转bitmap
75
70
*
76
71
* @param bytes 字节数组
77
- * @return bitmap对象
72
+ * @return bitmap
78
73
*/
79
74
public static Bitmap bytes2Bitmap (byte [] bytes ) {
80
75
return ConvertUtils .bytes2Bitmap (bytes );
@@ -84,7 +79,7 @@ public static Bitmap bytes2Bitmap(byte[] bytes) {
84
79
* drawable转bitmap
85
80
*
86
81
* @param drawable drawable对象
87
- * @return bitmap对象
82
+ * @return bitmap
88
83
*/
89
84
public static Bitmap drawable2Bitmap (Drawable drawable ) {
90
85
return ConvertUtils .drawable2Bitmap (drawable );
@@ -95,7 +90,7 @@ public static Bitmap drawable2Bitmap(Drawable drawable) {
95
90
*
96
91
* @param res resources对象
97
92
* @param bitmap bitmap对象
98
- * @return drawable对象
93
+ * @return drawable
99
94
*/
100
95
public static Drawable bitmap2Drawable (Resources res , Bitmap bitmap ) {
101
96
return ConvertUtils .bitmap2Drawable (res , bitmap );
@@ -117,12 +112,22 @@ public static byte[] drawable2Bytes(Drawable drawable, CompressFormat format) {
117
112
*
118
113
* @param res resources对象
119
114
* @param bytes 字节数组
120
- * @return drawable对象
115
+ * @return drawable
121
116
*/
122
117
public static Drawable bytes2Drawable (Resources res , byte [] bytes ) {
123
118
return ConvertUtils .bytes2Drawable (res , bytes );
124
119
}
125
120
121
+ /**
122
+ * view转Bitmap
123
+ *
124
+ * @param view 视图
125
+ * @return bitmap
126
+ */
127
+ public static Bitmap view2Bitmap (View view ) {
128
+ return ConvertUtils .view2Bitmap (view );
129
+ }
130
+
126
131
/**
127
132
* 计算采样大小
128
133
*
@@ -278,39 +283,6 @@ public static Bitmap getBitmap(byte[] data, int offset, int maxWidth, int maxHei
278
283
return BitmapFactory .decodeByteArray (data , offset , data .length , options );
279
284
}
280
285
281
- /**
282
- * 获取bitmap
283
- *
284
- * @param context 上下文
285
- * @param resId 资源id
286
- * @return bitmap
287
- */
288
- public static Bitmap getBitmap (Context context , int resId ) {
289
- if (context == null ) return null ;
290
- InputStream is = context .getResources ().openRawResource (resId );
291
- return BitmapFactory .decodeStream (is );
292
- }
293
-
294
- /**
295
- * 获取bitmap
296
- *
297
- * @param context 上下文
298
- * @param resId 资源id
299
- * @param maxWidth 最大宽度
300
- * @param maxHeight 最大高度
301
- * @return bitmap
302
- */
303
- public static Bitmap getBitmap (Context context , int resId , int maxWidth , int maxHeight ) {
304
- if (context == null ) return null ;
305
- BitmapFactory .Options options = new BitmapFactory .Options ();
306
- options .inJustDecodeBounds = true ;
307
- InputStream is = context .getResources ().openRawResource (resId );
308
- BitmapFactory .decodeStream (is , null , options );
309
- options .inSampleSize = calculateInSampleSize (options , maxWidth , maxHeight );
310
- options .inJustDecodeBounds = false ;
311
- return BitmapFactory .decodeStream (is , null , options );
312
- }
313
-
314
286
/**
315
287
* 获取bitmap
316
288
*
@@ -606,15 +578,14 @@ public static Bitmap toRound(Bitmap src, boolean recycle) {
606
578
int width = src .getWidth ();
607
579
int height = src .getHeight ();
608
580
int radius = Math .min (width , height ) >> 1 ;
609
- Bitmap ret = src . copy ( src .getConfig (), true );
581
+ Bitmap ret = Bitmap . createBitmap ( width , height , src .getConfig ());
610
582
Paint paint = new Paint ();
611
583
Canvas canvas = new Canvas (ret );
612
584
Rect rect = new Rect (0 , 0 , width , height );
613
585
paint .setAntiAlias (true );
614
- paint .setColor (Color .TRANSPARENT );
615
- paint .setXfermode (new PorterDuffXfermode (PorterDuff .Mode .SRC_IN ));
616
586
canvas .drawARGB (0 , 0 , 0 , 0 );
617
587
canvas .drawCircle (width >> 1 , height >> 1 , radius , paint );
588
+ paint .setXfermode (new PorterDuffXfermode (PorterDuff .Mode .SRC_IN ));
618
589
canvas .drawBitmap (src , rect , rect , paint );
619
590
if (recycle && !src .isRecycled ()) src .recycle ();
620
591
return ret ;
@@ -643,15 +614,15 @@ public static Bitmap toRoundCorner(Bitmap src, float radius, boolean recycle) {
643
614
if (null == src ) return null ;
644
615
int width = src .getWidth ();
645
616
int height = src .getHeight ();
646
- Bitmap ret = src .copy (src .getConfig (), true );
647
- BitmapShader bitmapShader = new BitmapShader (src ,
648
- Shader .TileMode .CLAMP , Shader .TileMode .CLAMP );
617
+ Bitmap ret = Bitmap .createBitmap (width , height , src .getConfig ());
649
618
Paint paint = new Paint ();
650
619
Canvas canvas = new Canvas (ret );
651
- RectF rectf = new RectF (0 , 0 , width , height );
620
+ Rect rect = new Rect (0 , 0 , width , height );
652
621
paint .setAntiAlias (true );
653
- paint .setShader (bitmapShader );
654
- canvas .drawRoundRect (rectf , radius , radius , paint );
622
+ canvas .drawARGB (0 , 0 , 0 , 0 );
623
+ canvas .drawRoundRect (new RectF (rect ), radius , radius , paint );
624
+ paint .setXfermode (new PorterDuffXfermode (PorterDuff .Mode .SRC_IN ));
625
+ canvas .drawBitmap (src , rect , rect , paint );
655
626
if (recycle && !src .isRecycled ()) src .recycle ();
656
627
return ret ;
657
628
}
@@ -671,7 +642,7 @@ public static Bitmap fastBlur(Context context, Bitmap src, float scale, float ra
671
642
}
672
643
673
644
/**
674
- * 快速模糊
645
+ * 快速模糊图片
675
646
* <p>先缩小原图,对小图进行模糊,再放大回原先尺寸</p>
676
647
*
677
648
* @param context 上下文
@@ -699,7 +670,7 @@ public static Bitmap fastBlur(Context context, Bitmap src, float scale, float ra
699
670
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN_MR1 ) {
700
671
scaleBitmap = renderScriptBlur (context , scaleBitmap , radius );
701
672
} else {
702
- scaleBitmap = stackBlur (scaleBitmap , (int ) radius , true );
673
+ scaleBitmap = stackBlur (scaleBitmap , (int ) radius , recycle );
703
674
}
704
675
if (scale == 1 ) return scaleBitmap ;
705
676
Bitmap ret = Bitmap .createScaledBitmap (scaleBitmap , width , height , true );
@@ -714,7 +685,7 @@ public static Bitmap fastBlur(Context context, Bitmap src, float scale, float ra
714
685
*
715
686
* @param context 上下文
716
687
* @param src 源图片
717
- * @param radius 模糊度(0 ...25)
688
+ * @param radius 模糊度(1 ...25)
718
689
* @return 模糊后的图片
719
690
*/
720
691
@ TargetApi (Build .VERSION_CODES .JELLY_BEAN_MR1 )
@@ -751,7 +722,7 @@ public static Bitmap renderScriptBlur(Context context, Bitmap src, float radius)
751
722
* @param src 源图片
752
723
* @param radius 模糊半径
753
724
* @param recycle 是否回收
754
- * @return stackBlur模糊图片
725
+ * @return stack模糊后的图片
755
726
*/
756
727
public static Bitmap stackBlur (Bitmap src , int radius , boolean recycle ) {
757
728
Bitmap ret ;
@@ -762,7 +733,7 @@ public static Bitmap stackBlur(Bitmap src, int radius, boolean recycle) {
762
733
}
763
734
764
735
if (radius < 1 ) {
765
- return ( null ) ;
736
+ return null ;
766
737
}
767
738
768
739
int w = ret .getWidth ();
@@ -954,7 +925,7 @@ public static Bitmap stackBlur(Bitmap src, int radius, boolean recycle) {
954
925
}
955
926
}
956
927
ret .setPixels (pix , 0 , w , 0 , 0 , w , h );
957
- return ( ret ) ;
928
+ return ret ;
958
929
}
959
930
960
931
/**
@@ -966,7 +937,7 @@ public static Bitmap stackBlur(Bitmap src, int radius, boolean recycle) {
966
937
* @return 带颜色边框图
967
938
*/
968
939
public static Bitmap addFrame (Bitmap src , int borderWidth , int color ) {
969
- return addFrame (src , borderWidth , color );
940
+ return addFrame (src , borderWidth , color , false );
970
941
}
971
942
972
943
/**
@@ -980,16 +951,18 @@ public static Bitmap addFrame(Bitmap src, int borderWidth, int color) {
980
951
*/
981
952
public static Bitmap addFrame (Bitmap src , int borderWidth , int color , boolean recycle ) {
982
953
if (isEmptyBitmap (src )) return null ;
983
- int newWidth = src .getWidth () + borderWidth >> 1 ;
984
- int newHeight = src .getHeight () + borderWidth >> 1 ;
954
+ int doubleBorder = borderWidth << 1 ;
955
+ int newWidth = src .getWidth () + doubleBorder ;
956
+ int newHeight = src .getHeight () + doubleBorder ;
985
957
Bitmap ret = Bitmap .createBitmap (newWidth , newHeight , src .getConfig ());
986
958
Canvas canvas = new Canvas (ret );
987
- Rect rec = canvas . getClipBounds ( );
959
+ Rect rect = new Rect ( 0 , 0 , newWidth , newHeight );
988
960
Paint paint = new Paint ();
989
961
paint .setColor (color );
990
962
paint .setStyle (Paint .Style .STROKE );
991
- paint .setStrokeWidth (borderWidth );
992
- canvas .drawRect (rec , paint );
963
+ // setStrokeWidth是居中画的,所以要两倍的宽度才能画,否则有一半的宽度是空的
964
+ paint .setStrokeWidth (doubleBorder );
965
+ canvas .drawRect (rect , paint );
993
966
canvas .drawBitmap (src , borderWidth , borderWidth , null );
994
967
if (recycle && !src .isRecycled ()) src .recycle ();
995
968
return ret ;
@@ -1053,13 +1026,13 @@ public static Bitmap addReflection(Bitmap src, int reflectionHeight, boolean rec
1053
1026
* @param content 水印文本
1054
1027
* @param textSize 水印字体大小
1055
1028
* @param color 水印字体颜色
1056
- * @param alpha 水印字体透明度
1057
1029
* @param x 起始坐标x
1058
1030
* @param y 起始坐标y
1059
1031
* @return 带有文字水印的图片
1060
1032
*/
1061
- public static Bitmap addTextWatermark (Bitmap src , String content , int textSize , int color , int alpha , float x , float y ) {
1062
- return addTextWatermark (src , content , textSize , color , alpha , x , y , false );
1033
+ public static Bitmap addTextWatermark (Bitmap src , String content , int textSize , int color , float x ,
1034
+ float y ) {
1035
+ return addTextWatermark (src , content , textSize , color , x , y , false );
1063
1036
}
1064
1037
1065
1038
/**
@@ -1069,23 +1042,22 @@ public static Bitmap addTextWatermark(Bitmap src, String content, int textSize,
1069
1042
* @param content 水印文本
1070
1043
* @param textSize 水印字体大小
1071
1044
* @param color 水印字体颜色
1072
- * @param alpha 水印字体透明度
1073
1045
* @param x 起始坐标x
1074
1046
* @param y 起始坐标y
1075
1047
* @param recycle 是否回收
1076
1048
* @return 带有文字水印的图片
1077
1049
*/
1078
- public static Bitmap addTextWatermark (Bitmap src , String content , int textSize , int color , int alpha , float x , float y , boolean recycle ) {
1050
+ public static Bitmap addTextWatermark (Bitmap src , String content , float textSize , int color , float x ,
1051
+ float y , boolean recycle ) {
1079
1052
if (isEmptyBitmap (src ) || content == null ) return null ;
1080
1053
Bitmap ret = src .copy (src .getConfig (), true );
1081
1054
Paint paint = new Paint (Paint .ANTI_ALIAS_FLAG );
1082
1055
Canvas canvas = new Canvas (ret );
1083
- paint .setAlpha (alpha );
1084
1056
paint .setColor (color );
1085
1057
paint .setTextSize (textSize );
1086
1058
Rect bounds = new Rect ();
1087
1059
paint .getTextBounds (content , 0 , content .length (), bounds );
1088
- canvas .drawText (content , x , y , paint );
1060
+ canvas .drawText (content , x , y + textSize , paint );
1089
1061
if (recycle && !src .isRecycled ()) src .recycle ();
1090
1062
return ret ;
1091
1063
}
@@ -1135,7 +1107,7 @@ public static Bitmap addImageWatermark(Bitmap src, Bitmap watermark, int x, int
1135
1107
* @return alpha位图
1136
1108
*/
1137
1109
public static Bitmap toAlpha (Bitmap src ) {
1138
- return toAlpha (src );
1110
+ return toAlpha (src , false );
1139
1111
}
1140
1112
1141
1113
/**
0 commit comments