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

xml:

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" >

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" >

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 mImageResArrayList = new ArrayList(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仿微信朋友圈点击加号添加图片功能相关推荐

  1. android 微信评论功能,Android仿微信朋友圈点击评论自动定位到相关行功能

    最近闲来无事,随便看看各种UI实现的代码 打开你的微信朋友圈,点击评论,你就会发现有一个小细节:文本输入框的高度恰好定位到这条信息的底部位置 这个实现起来其实很简单,咱们就来看看吧 最简单的Recyc ...

  2. android 微信评论功能,Android仿微信朋友圈点击评论自动定位到相关行功能.pdf

    Android仿仿微微信信朋朋友友圈圈点点击击评评论论自自动动定定位位到到相相关关行行功功能能 这篇文章主要介绍了android仿微信朋友圈点击评论自动定位到相关行功能的实现,本文图文并茂给大家介绍的 ...

  3. android 查找朋友圈,Android仿微信朋友圈点击评论自动定位到相关行功能

    最近闲来无事,随便看看各种UI实现的代码 打开你的微信朋友圈,点击评论,你就会发现有一个小细节:文本输入框的高度恰好定位到这条信息的底部位置 这个实现起来其实很简单,咱们就来看看吧 最简单的Recyc ...

  4. 仿微信朋友圈点击评论自动定位到对应位置

    参考文章:仿微信朋友圈点击评论自动定位到相关行 一.思路: 1.在点击[评论]控件的时候弹出一个 dialog,dialog 上面为 ScrollView,下面为输入框,实现键盘弹出时把输入框顶上去的 ...

  5. Android仿微信朋友圈图片展示效果,图片查看器

    现在越来越多的APP都会有图片展示,这里是模仿微信朋友圈图片展示效果,图片查看器. 主要分为4部分: 1.透明Activity 2.计算gridView下iamgeView Item所在位置 3.一张 ...

  6. Android 实现仿微信朋友圈全文、收起的功能

    一.测试 实现: 二.准备工作 1.RecyclerView的适配器ExpandFoldTextAdapter.java public class ExpandFoldTextAdapter exte ...

  7. android 拖动控件删除,Android学习------拖动删除(仿微信朋友圈拖动删除)

    1 示例 发朋友圈的时候,长按图片可以调整顺序,还可以拖动到底部删除. 2 官方示例: https://developer.android.google.cn/guide/topics/ui/drag ...

  8. Android 仿微信朋友圈9宫格图片展示多选图片

    转载请标明出处: http://blog.csdn.net/airsaid/article/details/51026447 本文出自:[周游的博客] 申明 该项目后续没有维护,并且里面存在一些 Bu ...

  9. android通讯录项目_Flutter高仿微信项目开源-具即时通讯IM功能

    项目地址:https://github.com/fluttercandies/wechat_flutter wechat_flutter Flutter版本微信 效果图: 下载体验(Android): ...

最新文章

  1. C#结合js 上传文件和删除文件(技术点有:asp.net mvc ,nhibernate,ajax等)
  2. python分析nginx日志
  3. MIME Type的介绍
  4. 随机森林算法原理_集成算法,随机森林和梯度增强机
  5. Science | 郑文山等发布微生物组单细胞测序新技术Microbe-seq(王军/戴磊/晁彦杰点评)...
  6. 测量电流传感器的放大倍数
  7. Flash extractor功能介绍
  8. android adapter 组件,Android UI - AdapterView 及其子类
  9. 【修色圣典】第一章 色彩、对比度和通道
  10. 出走海报,立体感搞出来
  11. 7月第1周回顾:AMD英特尔四核战在即 华为3Com遭收购传言
  12. MySQL是怎样运行的
  13. Kafka 中的这些设计思想值得一学!
  14. 面试必问之JVM原理 1
  15. JetBrains全家桶安装、学生认证以及IDE中文设置
  16. 罗技驱动退出宏失效怎么办?
  17. 解决小程序自定义弹窗滚动穿透问题
  18. MySQL-- ADD COLUMN 添加多个字段的写法
  19. 2021-08-31【JVM系列-3】类文件
  20. 为什么把牛奶倒入大海?

热门文章

  1. ajax实现向上正在加载,向上滚动或者向下滚动分页异步加载数据(Ajax + lazyload)
  2. mysql 云无忧ps教程_华为云数据库MySQL一键开通读写分离,无忧应对企业业务高峰情景...
  3. Qt图形界面编程入门(3)
  4. linux流式访问日志,流式实时日志分析系统的实现原理
  5. CENTOS5下VSFTPD的设置
  6. MIP 支付组件,支付流程:
  7. 来自我的破船大大的博客,记录他的iOS成长之路,与君同勉!
  8. J2EE基础之Web服务简介
  9. Ural 1627 Join(生成树计数)
  10. 华为智能手环智能手表软件测试,一块智能手表的测试之旅,揭秘华为运动健康科学实验室...