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

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

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

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

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 vIEwsint hSpace = Utils.dptopx(10,getResources());// vertical space among children vIEwsint vSpace = Utils.dptopx(10,getResources()); // every child vIEw wIDth and height.int chilDWIDth = 0;int childHeight = 0; // store images res IDArrayList<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,defStyle);  TypedArray t = context.obtainStyledAttributes(attrs,R.styleable.NinePhotoVIEw,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

@OverrIDeprotected 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

@OverrIDeprotected 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张时,默认最后一张是+号图片,然后设置点击事件,d出对话框供用户选择 *** 作。

Draw

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

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

总结

以上是内存溢出为你收集整理的Android仿微信朋友圈点击加号添加图片功能全部内容,希望文章能够帮你解决Android仿微信朋友圈点击加号添加图片功能所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存