全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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微信朋友圈添加图片  # Android朋友圈点击加图片  # Android微信添加图片  # Android GridView扩展仿微信微博发图动态添加删除图片功能  # Android仿微信朋友圈添加图片的实例代码  # [Android] 通过GridView仿微信动态添加本地图片示例代码  # Android 高仿微信朋友圈动态支持双击手势放大并滑动查看图片效果  # Android制作微信添加多个图片放大图片功能  # 很好  # 都是  # 不需要  # 弹出  # 自定义  # 重写  # 大家分享  # 二个  # 对话框  # 具体内容  # 大家多多  # ViewGroup  # int  # static  # final  # drawable  # constImageIds  # MAX_PHOTO_NUMBER  # private  # ninephoto_vspace 


相关文章: 北京制作网站的公司排名,北京三快科技有限公司是做什么?北京三快科技?  如何选择长沙网站建站模板?H5响应式与品牌定制哪个更优?  齐河建站公司:营销型网站建设与SEO优化双核驱动策略  个人摄影网站制作流程,摄影爱好者都去什么网站?  深圳防火门网站制作公司,深圳中天明防火门怎么编码?  制作网站怎么制作,*游戏网站怎么搭建?  ui设计制作网站有哪些,手机UI设计网址吗?  如何自定义建站之星网站的导航菜单样式?  网站图片在线制作软件,怎么在图片上做链接?  阿里云网站制作公司,阿里云快速搭建网站好用吗?  Thinkphp 中 distinct 的用法解析  矢量图网站制作软件,用千图网的一张矢量图做公司app首页,该网站并未说明版权等问题,这样做算不算侵权?应该如何解决?  建站之星展会模板:智能建站与自助搭建高效解决方案  全景视频制作网站有哪些,全景图怎么做成网页?  七夕网站制作视频,七夕大促活动怎么报名?  做企业网站制作流程,企业网站制作基本流程有哪些?  建站之星安装失败:服务器环境不兼容?  婚礼视频制作网站,学习*后期制作的网站有哪些?  建站主机SSH密钥生成步骤及常见问题解答?  Bpmn 2.0的XML文件怎么画流程图  建站之星如何助力企业快速打造五合一网站?  建站之星CMS五站合一模板配置与SEO优化指南  如何选择可靠的免备案建站服务器?  开源网站制作软件,开源网站什么意思?  如何在企业微信快速生成手机电脑官网?  网站好制作吗知乎,网站开发好学吗?有什么技巧?  如何通过虚拟机搭建网站?详细步骤解析  如何快速配置高效服务器建站软件?  香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化  标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?  英语简历制作免费网站推荐,如何将简历翻译成英文?  JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)  IOS倒计时设置UIButton标题title的抖动问题  详解jQuery中基本的动画方法  建站之星如何防范黑客攻击与数据泄露?  如何解决ASP生成WAP建站中文乱码问题?  建站之星各版本价格是多少?  如何通过智能用户系统一键生成高效建站方案?  小型网站建站如何选择虚拟主机?  建站上市公司网站建设方案与SEO优化服务定制指南  专业网站制作企业网站,如何制作一个企业网站,建设网站的基本步骤有哪些?  *服务器网站为何频现安全漏洞?  如何用美橙互联一键搭建多站合一网站?  如何在阿里云部署织梦网站?  免费制作统计图的网站有哪些,如何看待现如今年轻人买房难的情况?  中山网站推广排名,中山信息港登录入口?  ,网站推广常用方法?  网站制作网站,深圳做网站哪家比较好?  如何通过老薛主机一键快速建站?  智能起名网站制作软件有哪些,制作logo的软件? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。