发布于 2017-09-25 23:06:42 | 75 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Andorid开发之Picasso通过URL获取用户头像的圆形显示,非常不错,具有参考借鉴价值,需要的朋友可以参考下

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, 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类--压缩图片


//实现图片的压缩处理
//设置宽高必须使用浮点型,否则导致压缩的比例:0
public 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);

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务