Android仿微信群聊头像

Android仿微信群聊头像,第1张

概述工作中需要实现仿钉钉群头像的一个功能,就是个人的头像拼到一起显示,看了一下市场上的APP好像微信的群聊头像是组合的,QQ的头像不是,别的好像也没有了。

工作中需要实现仿钉钉群头像的一个功能,就是个人的头像拼到一起显示,看了一下市场上的APP好像微信的群聊头像是组合的,QQ的头像不是,别的好像也没有了。

给大家分享一下怎么实现的吧。首先我们先看一下效果图:

好了,下面说一下具体怎么实现的:

实现思路

1.首先获取Bitmap图片(本地、网络) 2.创建一个指定大小的缩略图 3.组合Bitmap图片

很简单,本地图片需要我们从本地读取,如果是网络图片我们也可以根据URL来获取bitmap进行组合

具体实现过程

1.布局文件:

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent"  androID:layout_height="match_parent" androID:paddingleft="@dimen/activity_horizontal_margin"  androID:paddingRight="@dimen/activity_horizontal_margin"  androID:paddingtop="@dimen/activity_vertical_margin"  androID:gravity="center"  androID:orIEntation="vertical"  androID:background="#987"  androID:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  <TextVIEw    androID:background="#fff"    androID:layout_wIDth="match_parent"    androID:layout_height="1dp"/>  <ImageVIEw    androID:src="@drawable/j"    androID:ID="@+ID/iv1"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content" />  <TextVIEw    androID:background="#fff"    androID:layout_wIDth="match_parent"    androID:layout_height="1dp"/>  <ImageVIEw    androID:src="@drawable/j"    androID:ID="@+ID/iv2"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content" />  <TextVIEw    androID:background="#fff"    androID:layout_wIDth="match_parent"    androID:layout_height="1dp"/>  <ImageVIEw    androID:src="@drawable/j"    androID:ID="@+ID/iv3"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content" />  <TextVIEw    androID:background="#fff"    androID:layout_wIDth="match_parent"    androID:layout_height="1dp"/>  <ImageVIEw    androID:src="@drawable/j"    androID:ID="@+ID/iv4"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content" />  <TextVIEw    androID:background="#fff"    androID:layout_wIDth="match_parent"    androID:layout_height="1dp"/></linearLayout>

四个ImageVIEw控件,用来显示图片不说了

2.获取Bitmap,设定图片的属性

/**   * 获取图片数组实体   * @param count   * @return   */  private List<BitmapBean> getBitmapEntitys(int count) {    List<BitmapBean> mList = new ArrayList<>();    String value = PropertIEsUtil.readData(this,String.valueOf(count),R.raw.data);    String[] arr1 = value.split(";");    int length = arr1.length;    for (int i = 0; i < length; i++) {      String content = arr1[i];      String[] arr2 = content.split(",");      BitmapBean entity = null;      for (int j = 0; j < arr2.length; j++) {        entity = new BitmapBean();        entity.setX(float.valueOf(arr2[0]));        entity.setY(float.valueOf(arr2[1]));        entity.setWIDth(float.valueOf(arr2[2]));        entity.setHeight(float.valueOf(arr2[3]));      }      mList.add(entity);    }    return mList;  }

3.创建压缩图片,这里我们用到了thumbnailUtils中的extractthumbnail()方法,参数为bitmap,wIDth,height

/**   * 初始化数据   */  private voID initData(){    /*获取四个图片数组*/    bitmapBeans1 = getBitmapEntitys(1);    bitmapBeans2 = getBitmapEntitys(2);    bitmapBeans3 = getBitmapEntitys(3);    bitmapBeans4 = getBitmapEntitys(4);    /*bitmap缩略图*/    Bitmap[] bitmaps1 = {        thumbnailUtils.extractthumbnail(BitmapUtils.getScaleBitmap(        getResources(),R.drawable.j),(int) bitmapBeans1        .get(0).getWIDth(),(int) bitmapBeans1.get(0).getWIDth())};    Bitmap[] bitmaps2 = {        thumbnailUtils.extractthumbnail(BitmapUtils.getScaleBitmap(        getResources(),(int) bitmapBeans2        .get(0).getWIDth(),(int) bitmapBeans2.get(0).getWIDth()),thumbnailUtils.extractthumbnail(BitmapUtils.getScaleBitmap(        getResources(),(int) bitmapBeans2.get(0).getWIDth())};    Bitmap[] bitmaps3 = {        thumbnailUtils.extractthumbnail(BitmapUtils.getScaleBitmap(        getResources(),(int) bitmapBeans3        .get(0).getWIDth(),(int) bitmapBeans3.get(0).getWIDth()),thumbnailUtils.extractthumbnail(BitmapUtils.getScaleBitmap(            getResources(),(int) bitmapBeans3            .get(0).getWIDth(),(int) bitmapBeans3.get(0).getWIDth())};    Bitmap[] bitmaps4 = {        thumbnailUtils.extractthumbnail(BitmapUtils.getScaleBitmap(            getResources(),(int) bitmapBeans4            .get(0).getWIDth(),(int) bitmapBeans4.get(0).getWIDth()),(int) bitmapBeans4.get(0).getWIDth())}; }

4.组合bitmap图片(也就是将我们的图片用Canvas画到一起)

/**   * 获得合在一起的bitmap   * @param mEntityList   * @param bitmaps   * @return   */  public static Bitmap getCombineBitmaps(List<BitmapBean> mEntityList,Bitmap... bitmaps) {    Bitmap newBitmap = Bitmap.createBitmap(200,200,Bitmap.Config.ARGB_8888);    for (int i = 0; i < mEntityList.size(); i++) {      bitmaps[i] = GetRoundedCornerBitmap(bitmaps[i]);      newBitmap = mixtureBitmap(newBitmap,bitmaps[i],new PointF(          mEntityList.get(i).getX(),mEntityList.get(i).getY()));    }    return newBitmap;  }

这里我为了好看将图片设置成圆形的了

/**   * 获取圆形的bitmap   * @param bitmap   * @return   */  public static Bitmap GetRoundedCornerBitmap(Bitmap bitmap) {    try {      Bitmap output = Bitmap.createBitmap(bitmap.getWIDth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);      Canvas canvas = new Canvas(output);      final Paint paint = new Paint();      final Rect rect = new Rect(0,bitmap.getWIDth(),bitmap.getHeight());      final RectF rectF = new RectF(new Rect(0,bitmap.getHeight()));      final float roundPx = 50;      paint.setAntiAlias(true);      canvas.drawARGB(0,0);      paint.setcolor(color.BLACK);      canvas.drawRoundRect(rectF,roundPx,paint);      paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));      final Rect src = new Rect(0,bitmap.getHeight());      canvas.drawBitmap(bitmap,src,rect,paint);      return output;    } catch (Exception e) {      return bitmap;    }  }

最后开画

 /**   * 画bitmap   * @param first   * @param second   * @param fromPoint   * @return   */  public static Bitmap mixtureBitmap(Bitmap first,Bitmap second,PointF fromPoint) {    if (first == null || second == null || fromPoint == null) {      return null;    }    Bitmap newBitmap = Bitmap.createBitmap(first.getWIDth(),first.getHeight(),Bitmap.Config.ARGB_8888);    Canvas cv = new Canvas(newBitmap);    cv.drawBitmap(first,null);    cv.drawBitmap(second,fromPoint.x,fromPoint.y,null);    cv.save(Canvas.ALL_SAVE_FLAG); //保存全部图层    cv.restore();    return newBitmap;  }

这样就简单的实现了微信群聊头像的效果,当然需要对图片做一些处理,已防止OOM,你也可以将它自定义成一个VIEw组件,小编有时间的话会实现这个的。
最后再给大家看一下小编项目上实现的效果吧,没啥区别,只不多数据源不一样了,是从网络上获取的。

这里写图片描述:

以上就是本文的全部内容,希望对大家的学习有所帮助。

总结

以上是内存溢出为你收集整理的Android仿微信群聊头像全部内容,希望文章能够帮你解决Android仿微信群聊头像所遇到的程序开发问题。

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

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

原文地址:https://54852.com/web/1142048.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存