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

Android仿微信朋友圈点击加号添加图片功能

程序员文章站 2022-08-02 14:25:46
本文为大家分享了类似微信朋友圈,点击+号图片,可以加图片功能,供大家参考,具体内容如下 xml:

本文为大家分享了类似微信朋友圈,点击+号图片,可以加图片功能,供大家参考,具体内容如下

Android仿微信朋友圈点击加号添加图片功能

xml:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margintop="40dp"
android:orientation="vertical" >
 
<com.sw.demo.widget.ninephotoview
  android:id="@+id/photoview"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  app:ninephoto_hspace="10dp"
  app:ninephoto_vspace="10dp"
  app:rainbowbar_color="@android:color/holo_blue_bright" >
 
</com.sw.demo.widget.ninephotoview>

ninephotoview.java

public class ninephotoview extends viewgroup {
 
public static final int max_photo_number = 9;
 
private int[] constimageids = { r.drawable.girl_0, r.drawable.girl_1,
   r.drawable.girl_2, r.drawable.girl_3, r.drawable.girl_4,
   r.drawable.girl_5, r.drawable.girl_6, r.drawable.girl_7,
   r.drawable.girl_8 };
 
// horizontal space among children views
int hspace = utils.dptopx(10, getresources());
// vertical space among children views
int vspace = utils.dptopx(10, getresources());
 
// every child view width and height.
int childwidth = 0;
int childheight = 0;
 
// store images res id
arraylist<integer> mimageresarraylist = new arraylist<integer>(9);
private view addphotoview;
 
public ninephotoview(context context) {
 super(context);
}
 
public ninephotoview(context context, attributeset attrs) {
 this(context, attrs, 0);
}
 
public ninephotoview(context context, attributeset attrs, int defstyle) {
 super(context, attrs, defstyle);
 
 typedarray t = context.obtainstyledattributes(attrs,
     r.styleable.ninephotoview, 0, 0);
 hspace = t.getdimensionpixelsize(
     r.styleable.ninephotoview_ninephoto_hspace, hspace);
 vspace = t.getdimensionpixelsize(
     r.styleable.ninephotoview_ninephoto_vspace, vspace);
 t.recycle();
 
 addphotoview = new view(context);
 addview(addphotoview);
 mimageresarraylist.add(new integer());
}

measure

@override
protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
 int rw = measurespec.getsize(widthmeasurespec);
 int rh = measurespec.getsize(heightmeasurespec);
 
 childwidth = (rw - 2 * hspace) / 3;
 childheight = childwidth;
 
 int childcount = this.getchildcount();
 for (int i = 0; i < childcount; i++) {
   view child = this.getchildat(i);
   //this.measurechild(child, widthmeasurespec, heightmeasurespec);
 
   layoutparams lparams = (layoutparams) child.getlayoutparams();
   lparams.left = (i % 3) * (childwidth + hspace);
   lparams.top = (i / 3) * (childwidth + vspace);
 }
 
 int vw = rw;
 int vh = rh;
 if (childcount < 3) {
   vw = childcount * (childwidth + hspace);
 }
 vh = ((childcount + 3) / 3) * (childwidth + vspace);
 setmeasureddimension(vw, vh);
}

  我们的子view三个一排,而且都是正方形,所以我们上面通过循环很好去得到所有子view的位置,注意我们上面把子view的左上角坐标存储到我们自定义的layoutparams 的left和top二个字段中,layout阶段会使用,最后我们算得整个viewgroup的宽高,调用setmeasureddimension设置。  

layout

@override
protected void onlayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
 int childcount = this.getchildcount();
 for (int i = 0; i < childcount; i++) {
   view child = this.getchildat(i);
   layoutparams lparams = (layoutparams) child.getlayoutparams();
   child.layout(lparams.left, lparams.top, lparams.left + childwidth,
       lparams.top + childheight);
 
   if (i == mimageresarraylist.size() - 1 && mimageresarraylist.size() != max_photo_number) {
     child.setbackgroundresource(r.drawable.add_photo);
     child.setonclicklistener(new view.onclicklistener() {
 
       @override
       public void onclick(view arg0) {
         addphotobtnclick();
       }
     });
   }else {
     child.setbackgroundresource(constimageids[i]);
     child.setonclicklistener(null);
   }
 }
}
 
public void addphoto() {
 if (mimageresarraylist.size() < max_photo_number) {
   view newchild = new view(getcontext());
   addview(newchild);
   mimageresarraylist.add(new integer());
   requestlayout();
   invalidate();
 }
}
 
public void addphotobtnclick() {
 final charsequence[] items = { "take photo", "photo from gallery" };
 
 alertdialog.builder builder = new alertdialog.builder(getcontext());
 builder.setitems(items, new dialoginterface.onclicklistener() {
 
   @override
   public void onclick(dialoginterface arg0, int arg1) {
     addphoto();
   }
 
 });
 builder.show();
}

  最核心的就是调用layout方法,根据我们measure阶段获得的layoutparams中的left和top字段,也很好对每个子view进行位置排列。然后判断在图片未达到最大值9张时,默认最后一张是+号图片,然后设置点击事件,弹出对话框供用户选择操作。

draw

不需要重写,使用viewgroup默认实现即可。

Android仿微信朋友圈点击加号添加图片功能

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