55import android .content .res .Resources ;
66import android .graphics .Bitmap ;
77import android .graphics .Bitmap .CompressFormat ;
8- import android .graphics .Bitmap .Config ;
98import android .graphics .BitmapFactory ;
10- import android .graphics .BitmapShader ;
119import android .graphics .Canvas ;
1210import android .graphics .Color ;
1311import android .graphics .ColorMatrix ;
2119import android .graphics .Rect ;
2220import android .graphics .RectF ;
2321import android .graphics .Shader ;
24- import android .graphics .drawable .BitmapDrawable ;
2522import android .graphics .drawable .Drawable ;
26- import android .hardware .camera2 .params .BlackLevelPattern ;
2723import android .media .ExifInterface ;
2824import android .os .Build ;
2925import android .renderscript .Allocation ;
3026import android .renderscript .Element ;
3127import android .renderscript .RenderScript ;
3228import android .renderscript .ScriptIntrinsicBlur ;
29+ import android .view .View ;
3330
3431import java .io .BufferedInputStream ;
3532import java .io .BufferedOutputStream ;
36- import java .io .ByteArrayInputStream ;
3733import java .io .ByteArrayOutputStream ;
3834import java .io .File ;
3935import java .io .FileDescriptor ;
4339import java .io .IOException ;
4440import java .io .InputStream ;
4541import java .io .OutputStream ;
46- import java .util .Date ;
4742
4843/**
4944 * <pre>
@@ -74,7 +69,7 @@ public static byte[] bitmap2Bytes(Bitmap bitmap, CompressFormat format) {
7469 * byteArr转bitmap
7570 *
7671 * @param bytes 字节数组
77- * @return bitmap对象
72+ * @return bitmap
7873 */
7974 public static Bitmap bytes2Bitmap (byte [] bytes ) {
8075 return ConvertUtils .bytes2Bitmap (bytes );
@@ -84,7 +79,7 @@ public static Bitmap bytes2Bitmap(byte[] bytes) {
8479 * drawable转bitmap
8580 *
8681 * @param drawable drawable对象
87- * @return bitmap对象
82+ * @return bitmap
8883 */
8984 public static Bitmap drawable2Bitmap (Drawable drawable ) {
9085 return ConvertUtils .drawable2Bitmap (drawable );
@@ -95,7 +90,7 @@ public static Bitmap drawable2Bitmap(Drawable drawable) {
9590 *
9691 * @param res resources对象
9792 * @param bitmap bitmap对象
98- * @return drawable对象
93+ * @return drawable
9994 */
10095 public static Drawable bitmap2Drawable (Resources res , Bitmap bitmap ) {
10196 return ConvertUtils .bitmap2Drawable (res , bitmap );
@@ -117,12 +112,22 @@ public static byte[] drawable2Bytes(Drawable drawable, CompressFormat format) {
117112 *
118113 * @param res resources对象
119114 * @param bytes 字节数组
120- * @return drawable对象
115+ * @return drawable
121116 */
122117 public static Drawable bytes2Drawable (Resources res , byte [] bytes ) {
123118 return ConvertUtils .bytes2Drawable (res , bytes );
124119 }
125120
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+
126131 /**
127132 * 计算采样大小
128133 *
@@ -278,39 +283,6 @@ public static Bitmap getBitmap(byte[] data, int offset, int maxWidth, int maxHei
278283 return BitmapFactory .decodeByteArray (data , offset , data .length , options );
279284 }
280285
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-
314286 /**
315287 * 获取bitmap
316288 *
@@ -606,15 +578,14 @@ public static Bitmap toRound(Bitmap src, boolean recycle) {
606578 int width = src .getWidth ();
607579 int height = src .getHeight ();
608580 int radius = Math .min (width , height ) >> 1 ;
609- Bitmap ret = src . copy ( src .getConfig (), true );
581+ Bitmap ret = Bitmap . createBitmap ( width , height , src .getConfig ());
610582 Paint paint = new Paint ();
611583 Canvas canvas = new Canvas (ret );
612584 Rect rect = new Rect (0 , 0 , width , height );
613585 paint .setAntiAlias (true );
614- paint .setColor (Color .TRANSPARENT );
615- paint .setXfermode (new PorterDuffXfermode (PorterDuff .Mode .SRC_IN ));
616586 canvas .drawARGB (0 , 0 , 0 , 0 );
617587 canvas .drawCircle (width >> 1 , height >> 1 , radius , paint );
588+ paint .setXfermode (new PorterDuffXfermode (PorterDuff .Mode .SRC_IN ));
618589 canvas .drawBitmap (src , rect , rect , paint );
619590 if (recycle && !src .isRecycled ()) src .recycle ();
620591 return ret ;
@@ -643,15 +614,15 @@ public static Bitmap toRoundCorner(Bitmap src, float radius, boolean recycle) {
643614 if (null == src ) return null ;
644615 int width = src .getWidth ();
645616 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 ());
649618 Paint paint = new Paint ();
650619 Canvas canvas = new Canvas (ret );
651- RectF rectf = new RectF (0 , 0 , width , height );
620+ Rect rect = new Rect (0 , 0 , width , height );
652621 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 );
655626 if (recycle && !src .isRecycled ()) src .recycle ();
656627 return ret ;
657628 }
@@ -671,7 +642,7 @@ public static Bitmap fastBlur(Context context, Bitmap src, float scale, float ra
671642 }
672643
673644 /**
674- * 快速模糊
645+ * 快速模糊图片
675646 * <p>先缩小原图,对小图进行模糊,再放大回原先尺寸</p>
676647 *
677648 * @param context 上下文
@@ -699,7 +670,7 @@ public static Bitmap fastBlur(Context context, Bitmap src, float scale, float ra
699670 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN_MR1 ) {
700671 scaleBitmap = renderScriptBlur (context , scaleBitmap , radius );
701672 } else {
702- scaleBitmap = stackBlur (scaleBitmap , (int ) radius , true );
673+ scaleBitmap = stackBlur (scaleBitmap , (int ) radius , recycle );
703674 }
704675 if (scale == 1 ) return scaleBitmap ;
705676 Bitmap ret = Bitmap .createScaledBitmap (scaleBitmap , width , height , true );
@@ -714,7 +685,7 @@ public static Bitmap fastBlur(Context context, Bitmap src, float scale, float ra
714685 *
715686 * @param context 上下文
716687 * @param src 源图片
717- * @param radius 模糊度(0 ...25)
688+ * @param radius 模糊度(1 ...25)
718689 * @return 模糊后的图片
719690 */
720691 @ TargetApi (Build .VERSION_CODES .JELLY_BEAN_MR1 )
@@ -751,7 +722,7 @@ public static Bitmap renderScriptBlur(Context context, Bitmap src, float radius)
751722 * @param src 源图片
752723 * @param radius 模糊半径
753724 * @param recycle 是否回收
754- * @return stackBlur模糊图片
725+ * @return stack模糊后的图片
755726 */
756727 public static Bitmap stackBlur (Bitmap src , int radius , boolean recycle ) {
757728 Bitmap ret ;
@@ -762,7 +733,7 @@ public static Bitmap stackBlur(Bitmap src, int radius, boolean recycle) {
762733 }
763734
764735 if (radius < 1 ) {
765- return ( null ) ;
736+ return null ;
766737 }
767738
768739 int w = ret .getWidth ();
@@ -954,7 +925,7 @@ public static Bitmap stackBlur(Bitmap src, int radius, boolean recycle) {
954925 }
955926 }
956927 ret .setPixels (pix , 0 , w , 0 , 0 , w , h );
957- return ( ret ) ;
928+ return ret ;
958929 }
959930
960931 /**
@@ -966,7 +937,7 @@ public static Bitmap stackBlur(Bitmap src, int radius, boolean recycle) {
966937 * @return 带颜色边框图
967938 */
968939 public static Bitmap addFrame (Bitmap src , int borderWidth , int color ) {
969- return addFrame (src , borderWidth , color );
940+ return addFrame (src , borderWidth , color , false );
970941 }
971942
972943 /**
@@ -980,16 +951,18 @@ public static Bitmap addFrame(Bitmap src, int borderWidth, int color) {
980951 */
981952 public static Bitmap addFrame (Bitmap src , int borderWidth , int color , boolean recycle ) {
982953 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 ;
985957 Bitmap ret = Bitmap .createBitmap (newWidth , newHeight , src .getConfig ());
986958 Canvas canvas = new Canvas (ret );
987- Rect rec = canvas . getClipBounds ( );
959+ Rect rect = new Rect ( 0 , 0 , newWidth , newHeight );
988960 Paint paint = new Paint ();
989961 paint .setColor (color );
990962 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 );
993966 canvas .drawBitmap (src , borderWidth , borderWidth , null );
994967 if (recycle && !src .isRecycled ()) src .recycle ();
995968 return ret ;
@@ -1053,13 +1026,13 @@ public static Bitmap addReflection(Bitmap src, int reflectionHeight, boolean rec
10531026 * @param content 水印文本
10541027 * @param textSize 水印字体大小
10551028 * @param color 水印字体颜色
1056- * @param alpha 水印字体透明度
10571029 * @param x 起始坐标x
10581030 * @param y 起始坐标y
10591031 * @return 带有文字水印的图片
10601032 */
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 );
10631036 }
10641037
10651038 /**
@@ -1069,23 +1042,22 @@ public static Bitmap addTextWatermark(Bitmap src, String content, int textSize,
10691042 * @param content 水印文本
10701043 * @param textSize 水印字体大小
10711044 * @param color 水印字体颜色
1072- * @param alpha 水印字体透明度
10731045 * @param x 起始坐标x
10741046 * @param y 起始坐标y
10751047 * @param recycle 是否回收
10761048 * @return 带有文字水印的图片
10771049 */
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 ) {
10791052 if (isEmptyBitmap (src ) || content == null ) return null ;
10801053 Bitmap ret = src .copy (src .getConfig (), true );
10811054 Paint paint = new Paint (Paint .ANTI_ALIAS_FLAG );
10821055 Canvas canvas = new Canvas (ret );
1083- paint .setAlpha (alpha );
10841056 paint .setColor (color );
10851057 paint .setTextSize (textSize );
10861058 Rect bounds = new Rect ();
10871059 paint .getTextBounds (content , 0 , content .length (), bounds );
1088- canvas .drawText (content , x , y , paint );
1060+ canvas .drawText (content , x , y + textSize , paint );
10891061 if (recycle && !src .isRecycled ()) src .recycle ();
10901062 return ret ;
10911063 }
@@ -1135,7 +1107,7 @@ public static Bitmap addImageWatermark(Bitmap src, Bitmap watermark, int x, int
11351107 * @return alpha位图
11361108 */
11371109 public static Bitmap toAlpha (Bitmap src ) {
1138- return toAlpha (src );
1110+ return toAlpha (src , false );
11391111 }
11401112
11411113 /**
0 commit comments