圆角头像的处理

图片处理类:


  1. public class ImageHelper {  
  2.     /** 
  3.      * 转换图片成圆形 
  4.      * @param bitmap 传入Bitmap对象 
  5.      * @return 
  6.      */  
  7.     public static Bitmap toRoundBitmap(Bitmap bitmap) {  
  8.             int width = bitmap.getWidth();  
  9.             int height = bitmap.getHeight();  
  10.             float roundPx;  
  11.             float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;  
  12.             if (width <= height) {  
  13.                     roundPx = width / 2;  
  14.                     top = 0;  
  15.                     bottom = width;  
  16.                     left = 0;  
  17.                     right = width;  
  18.                     height = width;  
  19.                     dst_left = 0;  
  20.                     dst_top = 0;  
  21.                     dst_right = width;  
  22.                     dst_bottom = width;  
  23.             } else {  
  24.                     roundPx = height / 2;  
  25.                     float clip = (width - height) / 2;  
  26.                     left = clip;  
  27.                     right = width - clip;  
  28.                     top = 0;  
  29.                     bottom = height;  
  30.                     width = height;  
  31.                     dst_left = 0;  
  32.                     dst_top = 0;  
  33.                     dst_right = height;  
  34.                     dst_bottom = height;  
  35.             }  
  36.               
  37.             Bitmap output = Bitmap. createBitmap(width,  
  38.                             height, Config. ARGB_8888 );  
  39.             Canvas canvas = new Canvas(output);  
  40.               
  41.             final int color = 0xff424242;  
  42.             final Paint paint = new Paint();  
  43.             final Rect src = new Rect(( int)left, ( int )top, ( int)right, (int )bottom);  
  44.             final Rect dst = new Rect(( int)dst_left, ( int )dst_top, (int )dst_right, ( int)dst_bottom);  
  45.             final RectF rectF = new RectF(dst);  
  46.   
  47.             paint.setAntiAlias( true );  
  48.               
  49.             canvas.drawARGB(0000);  
  50.             paint.setColor(color);  
  51.             canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
  52.   
  53.             paint.setXfermode( new PorterDuffXfermode(Mode. SRC_IN));  
  54.             canvas.drawBitmap(bitmap, src, dst, paint);  
  55.             return output;  
  56.     }  
  57.      
  58. }  


在Activity中调用:


  1. /* 将头像转为圆形 */  
  2.  Resources res = getResources();  
  3.  Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.head);  
  4.  head.setImageBitmap(ImageHelper.toRoundBitmap(bmp)); 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值