Andorid开发之Picasso通过URL获取用户头像的圆形显示

Andorid开发之Picasso通过URL获取用户头像的圆形显示,第1张

概述1.设置布局属性:<ImageViewandroid:scaleType=\"fitXY\"/>2.BitmapUtils类--得到指定圆形的Bitmap对象

1.设置布局属性:

<ImageVIEw androID:scaleType="fitXY"/>

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,paint); //设置图片相交情况下的处理方式 //setXfermode:设置当绘制的图像出现相交情况时候的处理方式的,它包含的常用模式有: //PorterDuff.Mode.SRC_IN 取两层图像交集部分,只显示上层图像 //PorterDuff.Mode.DST_IN 取两层图像交集部分,只显示下层图像 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); //在画布上绘制bitmap canvas.drawBitmap(source,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,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);

以上所述是小编给大家介绍的AndorID开发之Picasso通过URL获取用户头像的圆形显示,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Andorid开发之Picasso通过URL获取用户头像的圆形显示全部内容,希望文章能够帮你解决Andorid开发之Picasso通过URL获取用户头像的圆形显示所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1145911.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存