android 圆角图片 处理
2013-11-06 17:56
316 查看
在Activity中调用
/* 将头像转为圆形 */ Resources res = getResources(); Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.head); head.setImageBitmap(ImageHelper.toRoundBitmap(bmp));
//处理类 public class ImageHelper { /** * 转换图片成圆形 * @param bitmap 传入Bitmap对象 * @return */ public static Bitmap toRoundBitmap(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); float roundPx; float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom; if (width <= height) { roundPx = width / 2; top = 0; bottom = width; left = 0; right = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { roundPx = height / 2; float clip = (width - height) / 2; left = clip; right = width - clip; top = 0; bottom = height; width = height; dst_left = 0; dst_top = 0; dst_right = height; dst_bottom = height; } Bitmap output = Bitmap. createBitmap(width, height, Config. ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect src = new Rect((int)left, ( int)top, (int)right, (int)bottom); final Rect dst = new Rect((int)dst_left, ( int)dst_top, (int)dst_right, (int)dst_bottom); final RectF rectF = new RectF(dst); paint.setAntiAlias( true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode( new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, src, dst, paint); return output; } // 下载图片转为bitmap public static Bitmap getHttpBitmap(String url) { URL u = null; Bitmap bmp = null; try { u = new URL(url); } catch (Exception e) { // TODO: handle exception } try { HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.setConnectTimeout(0); conn.setDoInput( true); conn.connect(); InputStream ins = conn.getInputStream(); bmp = BitmapFactory. decodeStream(ins); ins.close(); } catch (Exception e) { // TODO: handle exception } return bmp; } }
相关文章推荐
- Android腾讯微薄客户端开发七:图片加圆角以及时间处理工具类
- Android图片处理——压缩、裁剪、圆角、保存
- android图片的缩放、圆角处理
- android图片圆角、图片去色处理示例
- Android中几种图像特效处理的小技巧,比如圆角,倒影,还有就是图片缩放,Drawable转化为Bitmap,Bitmap转化为Drawable等等
- Android图片缩放、圆角处理以及倒影特效代码
- android中处理图片成圆角
- android中图片的处理(绘制圆形,圆角||缩略图)
- android 背景圆角以及图片圆角处理
- android图片圆角处理,重写onDraw方法
- 【Android】Glide 实现图片再处理(比如在右下角添加 GIF 标识、圆角处理、添加水印等,通过在原 bitmap 的基础上进行再处理)
- Android 图片缩放,图片圆角处理
- android 图片圆角处理
- Android 利用多线程异步加载图,并把图片处理成圆角,并更新视图
- Android:继承ImageView 实现 圆角图片,圆形图片处理。
- android 指定角的圆角图片处理
- Android图片处理——压缩、剪裁、圆角、保存
- android 图片圆角处理
- android 图片圆角处理
- Android图片圆角处理