先求一个windows版本的gif制作工具!!!

这个代码只是做了简单的选择功能,图片的压缩与展示没做。可以自己在接口的方法中去实现。

写这个自定义view的初衷是为了学习LayoutParams,参考博客:http://www.jianshu.com/p/138b...

自定义一些属性

 <declare-styleable name="PhotoSelector"><attr name="columns" format="integer"/>//列<attr name="horizontal_space" format="dimension"/>//水平分割宽度<attr name="vertical_space" format="dimension"/>//竖直分割宽度</declare-styleable>

在布局中使用

 <com.idealcn.define.view.view.PhotoSelectorandroid:layout_width="match_parent"android:layout_height="wrap_content"ideal:columns="3"ideal:horizontal_space="3dp"ideal:vertical_space="3dp"><!-- 默认添加一个添加按钮的图片 --><ImageViewandroid:layout_width="100dp"android:layout_height="100dp"android:src="@drawable/ic_suggestions_add"/></com.idealcn.define.view.view.PhotoSelector>

自定义这个ViewGroup

public class PhotoSelector extends ViewGroup {public PhotoSelector(Context context) {this(context,null);}public PhotoSelector(Context context, AttributeSet attrs) {this(context, attrs,0);}public PhotoSelector(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}
}

获取默认的添加按钮

  @Overrideprotected void onFinishInflate() {super.onFinishInflate();//获取默认的添加按钮addView = getChildAt(0);viewList.add(addView);}

为添加按钮添加点击事件

 @Overrideprotected void onAttachedToWindow() {super.onAttachedToWindow();addView.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v) {//这里添加选择图片的代码  }});}

子View的测量和布局

这里要重写onMeasure和onLayout。定义ViewGroup.LayoutParams的子类来保存view的left和top的属性。

public static class PhotoParams extends LayoutParams{public int left,top;public PhotoParams(Context c, AttributeSet attrs) {super(c, attrs);}public PhotoParams(int width, int height) {super(width, height);}public PhotoParams(LayoutParams source) {super(source);}}

测量时,view的宽高一致,屏幕宽度减去水平分割线的宽度,然后平分给每个view。

     @Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);int width = MeasureSpec.getSize(widthMeasureSpec);int childWidth = (int) ((width - horizontalSpace * columns)/columns + .5f);int childHeight = childWidth;int childCount = viewList.size();for (int x = 0; x < childCount; x++) {//这里要从保存view的集合中取出viewView child = viewList.get(x);PhotoParams lp = (PhotoParams) child.getLayoutParams();if (lp==null) {lp = new PhotoParams(childWidth, childHeight);}lp.width = childWidth;lp.height = childHeight;//这里确定了view的left和top属性值,在onLayout中只需取出即可lp.left = x%columns * childWidth + (x%columns+1) * horizontalSpace;lp.top = x/columns * (childHeight + verticalSpace) + verticalSpace;child.setLayoutParams(lp);}if (childCount<=columns)setMeasuredDimension(childWidth*childCount,childHeight);else {int heightCount = childCount%columns!=0?(childCount/columns+1):childCount/columns;setMeasuredDimension(childWidth*columns,childHeight*heightCount);}//测量并确定子view的大小measureChildren(widthMeasureSpec,heightMeasureSpec);}

放置子view

 @Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {int childCount = viewList.size();for (int x = 0; x < childCount; x++) {View child = viewList.get(x);PhotoParams lp = (PhotoParams) child.getLayoutParams();child.layout(lp.left,lp.top,lp.left + child.getMeasuredWidth() + horizontalSpace,lp.top + child.getMeasuredHeight() + verticalSpace);}}

添加图片

定义一个List集合,添加的图片存放于集合中。然后调用requestLayout()。定义一个接口,将添加图片的操作交给所在的Activity处理。

定义接口

public interface OnAddPhotoListener {Bitmap addByCamera();//拍照Bitmap addByGallery();//相册
}

所在的Activity实现接口

public class TableActivity extends AppCompatActivity implements OnAddPhotoListener {...//省略一部分代码//只要把获取图片的操作在这里做处理即可@Overridepublic Bitmap addByCamera() {Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_round);return bitmap;}@Overridepublic Bitmap addByGallery() {Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_round);return bitmap;}
}

源码参考:https://github.com/idealcn/De...

自定义ViewGroup实现仿微信图片选择器相关推荐

  1. Android 仿微信图片选择器

    版权声明:本文为博主原创文章,未经博主允许不得转载. 1.自我介绍 这是我写的第一篇博客,首先做一下自我介绍,我去年刚毕业,大学学的是计算机专业,期间也学了一门Android相关课程,但是你懂的,一个 ...

  2. android仿微信图片选择器

    最近根据项目需求,要做一个仿微信图片选择的功能.首先我们先来整理一下思路. 1.显示选择图片的界面 1.1选择的图片数量小于9 最后一张图片是一个加号. 1.2选择的图片数量等于9,加号消失. 2.选 ...

  3. Android 超高仿微信图片选择器 图片该这么载入

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/39943731,本文出自: [张鸿洋的博客] 1.概述 关于手机图片载入器,在当 ...

  4. Android 超高仿微信图片选择器 图片该这么加载

    2019独角兽企业重金招聘Python工程师标准>>> 转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/39943 ...

  5. Android 仿微信图片选择器 PictureSelector3.0 的使用

    在做项目时经常会遇到图片选着,选择单张图片还好,但类似于微信发朋友圈时可以多图选择的时候,就有点手足无措.然后在网上看了很多类似的项目,也尝试过将他们用于自己的项目,比如 知乎开源图片选择库 Mati ...

  6. Android 超高仿微信图片选择器

    出处: http://blog.csdn.net/lmj623565791/article/details/39943731 1.概述 关于手机图片加载器,在当今像素随随便便破千万的时代,一张图片占据 ...

  7. android微信图片选择框架,Android仿微信图片选择器ImageSelector使用详解

    今天给大家介绍一个仿微信的图片选择器:ImageSelector.ImageSelector支持图片的单选.限数量的多选和不限数量的多选.支持图片预览和图片文件夹的切换.在上一篇文章 <Andr ...

  8. Android 超高仿微信图片选择器 图片该这么加载

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/39943731,本文出自: [张鸿洋的博客] 1.概述 关于手机图片加载器,在当 ...

  9. Android 高仿微信图片选择器

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/39943731,本文出自: [张鸿洋的博客] 1.概述 关于手机图片加载器,在当 ...

最新文章

  1. liunx上安装nacos
  2. 基于ArduinoLeonardo板子的BadUSB攻击实战
  3. python hashlib模块(提供常见摘要算法)
  4. scholar communication
  5. kirin710f是什么处理器_如何看待麒麟710F处理器?
  6. output怎么用_如何用 C++ 写一个可编程软件渲染器?
  7. 这是哪里来的小妖精!!!
  8. [渝粤教育] 中国矿业大学 恋爱心理学 参考 资料
  9. 2.3.0配置 spark_配置scala 2.11.12的spark-2.3.0 maven依赖项的问题
  10. saveOrUpdate的使用
  11. 在线教学视频的设计与实现
  12. 关于sql安装,升级,卸载时需要重启的解决方法
  13. 虚拟机 django 端口无法连接
  14. Long型转换成IP段String、StringIP段转换成Long型
  15. IE改善七大手法| ECRS工时分析软件
  16. AndroidStudio高级计算器三角函数对数
  17. 关于延拓定理的一点注解
  18. 低功耗计算机视觉技术前沿,四大方向,追求更小、更快、更高效
  19. base64 的加密和解密
  20. 贪吃蛇游戏 C语言程序设计

热门文章

  1. 数组各元素出现的次数
  2. 性能测试四十五:性能测试策略
  3. react项目在ie空白解决
  4. Hadoop点滴-HDFS命令行接口
  5. UEditor 插入图片大于2M提示文件大小超出范围解决办法
  6. 给linux用户加入sudo权限
  7. [No0000E6]C# 判断与循环
  8. 使用阿里云docker加速器
  9. WebService应用一例,带有安全验证
  10. 晴天php下载,x.php · 一步晴天/smart - Gitee.com