欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现朋友圈多图显示功能

程序员文章站 2022-10-08 23:13:45
本文实例为大家分享了android实现朋友圈多图显示的具体代码,供大家参考,具体内容如下 android实现朋友圈评论回复列表 android实现朋友圈点赞列表 an...

本文实例为大家分享了android实现朋友圈多图显示的具体代码,供大家参考,具体内容如下

android实现朋友圈评论回复列表

android实现朋友圈点赞列表

android实现朋友圈多图显示功能

正文

先看一下效果图:

Android实现朋友圈多图显示功能

multiimageview:

public class multiimageview extends linearlayout {
 public static int max_width = 0;

 // 照片的url列表
 private list<string> imageslist;

 /**
 * 长度 单位为pixel *
 */
 private int pxonemaxwandh; // 单张图最大允许宽高
 private int pxmorewandh = 0;// 多张图的宽高
 private int pximagepadding = lvdputil.dip2px(3);// 图片间的间距

 private int max_per_row_count = 3;// 每行显示最大数

 private layoutparams onepicpara;
 private layoutparams morepara, moreparacolumnfirst;
 private layoutparams rowpara;

 private onitemclicklistener monitemclicklistener;

 public void setonitemclicklistener(onitemclicklistener onitemclicklistener) {
 monitemclicklistener = onitemclicklistener;
 }

 public multiimageview(context context) {
 super(context);
 }

 public multiimageview(context context, attributeset attrs) {
 super(context, attrs);
 }

 public void setlist(list<string> lists) throws illegalargumentexception {
 if (lists == null) {
  throw new illegalargumentexception("imagelist is null...");
 }
 imageslist = lists;

 if (max_width > 0) {
  // 如果需要两张和四张图横向铺满,这里去掉注释即可。
//  if (lists.size() == 2 || lists.size() == 4) {
//  pxmorewandh = (max_width - pximagepadding) / 2;
//  } else {
  pxmorewandh = (max_width - pximagepadding * 2) / 3; //解决右侧图片和内容对不齐问题
//  }
  pxonemaxwandh = max_width * 2 / 3; // 一张图的时候,图片宽度
  initimagelayoutparams();
 }

 initview();
 }

 @override
 protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
 if (max_width == 0) {
  int width = measurewidth(widthmeasurespec);
  if (width > 0) {
  max_width = width - getpaddingleft() - getpaddingright();
  if (imageslist != null && imageslist.size() > 0) {
   setlist(imageslist);
  }
  }
 }
 super.onmeasure(widthmeasurespec, heightmeasurespec);
 }

 /**
 * determines the width of this view
 *
 * @param measurespec a measurespec packed into an int
 * @return the width of the view, honoring constraints from measurespec
 */
 private int measurewidth(int measurespec) {
 int result = 0;
 int specmode = measurespec.getmode(measurespec);
 int specsize = measurespec.getsize(measurespec);

 if (specmode == measurespec.exactly) {
  // we were told how big to be
  result = specsize;
 } else {
  // measure the text
  // result = (int) mtextpaint.measuretext(mtext) + getpaddingleft()
  // + getpaddingright();
  if (specmode == measurespec.at_most) {
  // respect at_most value if that was what is called for by
  // measurespec
  result = math.min(result, specsize);
  }
 }
 return result;
 }

 private void initimagelayoutparams() {
 int wrap = layoutparams.wrap_content;
 int match = layoutparams.match_parent;

 onepicpara = new layoutparams(pxonemaxwandh, wrap);

 moreparacolumnfirst = new layoutparams(pxmorewandh, pxmorewandh);
 morepara = new layoutparams(pxmorewandh, pxmorewandh);
 morepara.setmargins(pximagepadding, 0, 0, 0);

 rowpara = new layoutparams(match, wrap);
 }

 // 根据imageview的数量初始化不同的view布局,还要为每一个view作点击效果
 private void initview() {
 this.setorientation(vertical);
 this.removeallviews();
 if (max_width == 0) {
  //为了触发onmeasure()来测量multiimageview的最大宽度,multiimageview的宽设置为match_parent
  addview(new view(getcontext()));
  return;
 }

 if (imageslist == null || imageslist.size() == 0) {
  return;
 }

 if (imageslist.size() == 1) {
  addview(createimageview(0, false));
 } else {
  int allcount = imageslist.size();
  if (allcount == 4) {
  max_per_row_count = 2;
  } else {
  max_per_row_count = 3;
  }
  int rowcount = allcount / max_per_row_count
   + (allcount % max_per_row_count > 0 ? 1 : 0);// 行数
  for (int rowcursor = 0; rowcursor < rowcount; rowcursor++) {
  linearlayout rowlayout = new linearlayout(getcontext());
  rowlayout.setorientation(linearlayout.horizontal);

  rowlayout.setlayoutparams(rowpara);
  if (rowcursor != 0) {
   rowlayout.setpadding(0, pximagepadding, 0, 0);
  }

  int columncount = allcount % max_per_row_count == 0 ? max_per_row_count
   : allcount % max_per_row_count;//每行的列数
  if (rowcursor != rowcount - 1) {
   columncount = max_per_row_count;
  }
  addview(rowlayout);

  int rowoffset = rowcursor * max_per_row_count;// 行偏移
  for (int columncursor = 0; columncursor < columncount; columncursor++) {
   int position = columncursor + rowoffset;
   rowlayout.addview(createimageview(position, true));
  }
  }
 }
 }

 private imageview createimageview(final int position, final boolean ismultiimage) {
 string url = "";
 if (!textutils.isempty(imageslist.get(position))) {
  url = imageslist.get(position);
 }

 imageview imageview = new colorfilterimageview(getcontext());
 if (ismultiimage) {
  imageview.setscaletype(scaletype.center_crop);
  imageview.setlayoutparams(position % max_per_row_count == 0 ? moreparacolumnfirst : morepara);
 } else {
  imageview.setadjustviewbounds(true);
  imageview.setscaletype(scaletype.fit_start);
  imageview.setmaxheight(pxonemaxwandh);
  imageview.setlayoutparams(onepicpara);
 }

 imageview.setid(url.hashcode());
 imageview.setonclicklistener(new onclicklistener() {
  @override
  public void onclick(view v) {
  if (monitemclicklistener != null) {
   monitemclicklistener.onitemclick(v, position);
  }
  }
 });
 // 加载网络图片/设置图片显示
 glide.with(getcontext()).load(url).into(imageview);
 return imageview;
 }

 public interface onitemclicklistener {
 void onitemclick(view view, int position);
 }
}

点击有阴影的 imageview :

public class colorfilterimageview extends imageview implements ontouchlistener {
 public colorfilterimageview(context context) {
 this(context, null, 0);
 }

 public colorfilterimageview(context context, attributeset attrs) {
 this(context, attrs, 0);
 }

 public colorfilterimageview(context context, attributeset attrs, int defstyle) {
 super(context, attrs, defstyle);
 init();
 }

 private void init() {
 setontouchlistener(this);
 }

 @override
 public boolean ontouch(view v, motionevent event) {
 switch (event.getaction()) {
  case motionevent.action_down: // 按下时图像变灰
  setcolorfilter(color.gray, mode.multiply);
  break;
  case motionevent.action_up: // 手指离开或取消操作时恢复原色
  case motionevent.action_cancel:
  setcolorfilter(color.transparent);
  break;
  default:
  break;
 }
 return false;
 }
}

用法

<com.lvfq.myworkingtest.dynamic.view.multiimageview
 android:id="@+id/multi_image"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:padding="@dimen/dp_10" />

multiimageview multiimageview = findviewbyid(r.id.multi_image);
multiimageview.setlist(imgs); // list<string> 类型的图片地址列表
multiimage.setonitemclicklistener(new multiimageview.onitemclicklistener() {
   @override
   public void onitemclick(view view, int position) {
   // to do something or 查看大图.
   }
  });

代码已整理到github

附:如果需要完整朋友圈项目的话,这里推荐一个 github 项目仿微信实现的朋友圈

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