android 圆角图片 处理

在Activity中调用
[java]  view plain copy
  1. /* 将头像转为圆形 */  
  2.    Resources res = getResources();  
  3.    Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.head);  
  4.    head.setImageBitmap(ImageHelper.toRoundBitmap(bmp));  

  1. //处理类  
  2. public class ImageHelper {  
  3.     /** 
  4.      * 转换图片成圆形 
  5.      * @param bitmap 传入Bitmap对象 
  6.      * @return 
  7.      */  
  8.     public static Bitmap toRoundBitmap(Bitmap bitmap) {  
  9.             int width = bitmap.getWidth();  
  10.             int height = bitmap.getHeight();  
  11.             float roundPx;  
  12.             float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;  
  13.             if (width <= height) {  
  14.                     roundPx = width / 2;  
  15.                     top = 0;  
  16.                     bottom = width;  
  17.                     left = 0;  
  18.                     right = width;  
  19.                     height = width;  
  20.                     dst_left = 0;  
  21.                     dst_top = 0;  
  22.                     dst_right = width;  
  23.                     dst_bottom = width;  
  24.             } else {  
  25.                     roundPx = height / 2;  
  26.                     float clip = (width - height) / 2;  
  27.                     left = clip;  
  28.                     right = width - clip;  
  29.                     top = 0;  
  30.                     bottom = height;  
  31.                     width = height;  
  32.                     dst_left = 0;  
  33.                     dst_top = 0;  
  34.                     dst_right = height;  
  35.                     dst_bottom = height;  
  36.             }  
  37.               
  38.             Bitmap output = Bitmap. createBitmap(width,  
  39.                             height, Config. ARGB_8888);  
  40.             Canvas canvas = new Canvas(output);  
  41.               
  42.             final int color = 0xff424242;  
  43.             final Paint paint = new Paint();  
  44.             final Rect src = new Rect((int)left, ( int)top, (int)right, (int)bottom);  
  45.             final Rect dst = new Rect((int)dst_left, ( int)dst_top, (int)dst_right, (int)dst_bottom);  
  46.             final RectF rectF = new RectF(dst);  
  47.   
  48.             paint.setAntiAlias( true);  
  49.               
  50.             canvas.drawARGB(0000);  
  51.             paint.setColor(color);  
  52.             canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
  53.   
  54.             paint.setXfermode( new PorterDuffXfermode(Mode.SRC_IN));  
  55.             canvas.drawBitmap(bitmap, src, dst, paint);  
  56.             return output;  
  57.     }  
  58.      
  59.     // 下载图片转为bitmap  
  60.     public static Bitmap getHttpBitmap(String url) {  
  61.       URL u = null;  
  62.       Bitmap bmp = null;  
  63.        try {  
  64.           u = new URL(url);  
  65.   
  66.       } catch (Exception e) {  
  67.           // TODO: handle exception  
  68.       }  
  69.        try {  
  70.           HttpURLConnection conn = (HttpURLConnection) u.openConnection();  
  71.           conn.setConnectTimeout(0);  
  72.           conn.setDoInput( true);  
  73.           conn.connect();  
  74.           InputStream ins = conn.getInputStream();  
  75.           bmp = BitmapFactory. decodeStream(ins);  
  76.           ins.close();  
  77.       } catch (Exception e) {  
  78.           // TODO: handle exception  
  79.       }  
  80.        return bmp;  
  81.     }  
  82. }  

原文:http://blog.csdn.net/onlyonecoder/article/details/14231159

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值