博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Picasso通过URL获取--用户头像的圆形显示
阅读量:6955 次
发布时间:2019-06-27

本文共 1757 字,大约阅读时间需要 5 分钟。

1.设置布局属性:

2.BitmapUtils类-- 得到指定圆形的Bitmap对象

public static Bitmap circleBitmap(Bitmap source) {    //获取Bitmap的宽度    int width = source.getWidth();    //以Bitmap的宽度值作为新的bitmap的宽高值。    Bitmap bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);    //以此bitmap为基准,创建一个画布    Canvas canvas = new Canvas(bitmap);    Paint paint = new Paint();    paint.setAntiAlias(true);    //在画布上画一个圆    canvas.drawCircle(width / 2, width / 2, width / 2, paint);    //设置图片相交情况下的处理方式    //setXfermode:设置当绘制的图像出现相交情况时候的处理方式的,它包含的常用模式有:    //PorterDuff.Mode.SRC_IN 取两层图像交集部分,只显示上层图像    //PorterDuff.Mode.DST_IN 取两层图像交集部分,只显示下层图像    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));    //在画布上绘制bitmap    canvas.drawBitmap(source, 0, 0, paint);    return bitmap;}

3.BitmapUtils类--压缩图片

//实现图片的压缩处理//设置宽高必须使用浮点型,否则导致压缩的比例:0public static Bitmap zoom(Bitmap source,float width ,float height){    Matrix matrix = new Matrix();    //图片的压缩处理    matrix.postScale(width / source.getWidth(),height / source.getHeight());    Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, false);    return bitmap;}

4.根据user.getImageurl()显示圆形图像

//使用Picasso联网获取图片Picasso.with(this.getActivity()).load(user.getImageurl()).transform(new Transformation() {    @Override    public Bitmap transform(Bitmap source) {//下载以后的内存中的bitmap对象        //压缩处理        Bitmap bitmap = BitmapUtils.zoom(source, UIUtils.dp2px(62),UIUtils.dp2px(62));        //圆形处理        bitmap = BitmapUtils.circleBitmap(bitmap);        //回收bitmap资源        source.recycle();        return bitmap;    }    @Override    public String key() {        return "";//需要保证返回值不能为null。否则报错    }}).into(ivMeIcon);

  

  

  

转载于:https://www.cnblogs.com/ganchuanpu/p/7087776.html

你可能感兴趣的文章
jqueryMobile框架页面与页面切换
查看>>
对PostgreSQL中tablespace 与 database, table的理解
查看>>
图像编辑之一键特效(Lomo,反转负冲,柔光)
查看>>
linux modprobe命令参数及用法详解--linux加载模块命令
查看>>
从SQLSERVER/MYSQL数据库中随机取一条或者N条记录
查看>>
flex中dispatchEvent的用法(自定义事件) .
查看>>
html网页编码问题
查看>>
【Java】环境变量的配置
查看>>
排序算法——快速排序(转)
查看>>
上海交通大学2003年数学分析考研试题
查看>>
Qt搭建多线程Server
查看>>
Android应用公布的准备——生成渠道包
查看>>
Runtime(动态添加属性)
查看>>
反射中getMethods 与 getDeclaredMethods 的区别
查看>>
RSA加密的测试demo
查看>>
properties文件value换行处理方式
查看>>
docker学习笔记:修改无法启动的容器中的内容
查看>>
android中checkbox的padding引发的问题
查看>>
数据结构之栈的应用
查看>>
序列变换(二分,最小化最大值)
查看>>