仿微信朋友圈根据图片数量不同而显示不同格式
第一步:自定义控件
public class FriendsCircleImageLayout extends ViewGroup {/*** 显示的行数*/private int mColumnCount;/*** 默认间距*/private final float DEFAULT_SPACING = 2.5f;private float mSpacing;/*** 图片宽高比(党为多张图片的时候为1)* 一般在url中会有宽高 可计算*/private float mItemAspectRatio;/*** 最宽的时候相对可用空间比例(当childCount=1的时候)* 当只有一张图片的 最大显示宽度和高度相对于可用宽的的比例*/private final float MAX_WIDTH_PERCENTAGE = 270f / 350;private int mItemWidth;private int mItemHeight;public FriendsCircleImageLayout(Context context) {this(context, null);}public FriendsCircleImageLayout(Context context, AttributeSet attrs) {this(context, attrs, 0);}public FriendsCircleImageLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mSpacing = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_SPACING,context.getResources().getDisplayMetrics());}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int count = getChildCount();final int width = MeasureSpec.getSize(widthMeasureSpec);//当只有一张图片的时候 显示一张相对比较大的图片//当大于1小于等于4张图片的时候 一排显示两张图片//当只有一张图片的时候if (count == 1) {mColumnCount = 1;int mItemMaxWidth = (int) (width * MAX_WIDTH_PERCENTAGE);int mItemMaxHeight = mItemMaxWidth;if (mItemAspectRatio < 1) {mItemHeight = mItemMaxHeight;mItemWidth = (int) (mItemHeight * mItemAspectRatio);} else {mItemWidth = mItemMaxWidth;mItemHeight = (int) (mItemMaxWidth / mItemAspectRatio);}} else {if (count <= 4) {mColumnCount = 2;} else {mColumnCount = 3;}mItemWidth = (int) ((width - getPaddingLeft() - getPaddingRight() - 2 * mSpacing) / 3);mItemHeight = (int) (mItemWidth / mItemAspectRatio);}for (int i = 0; i < getChildCount(); i++) {View view = getChildAt(i);LayoutParams layoutParams = view.getLayoutParams();layoutParams.width = mItemWidth;layoutParams.height = mItemHeight;measureChild(view, widthMeasureSpec, heightMeasureSpec);}final int heightMode = MeasureSpec.getMode(heightMeasureSpec);if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {heightMeasureSpec = MeasureSpec.makeMeasureSpec(getDesiredHeight(mItemHeight), MeasureSpec.EXACTLY);}final int widthMode = MeasureSpec.getMode(widthMeasureSpec);if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {super.onMeasure(MeasureSpec.makeMeasureSpec(getDesiredWidth(mItemWidth), MeasureSpec.EXACTLY), heightMeasureSpec);} else {super.onMeasure(widthMeasureSpec, heightMeasureSpec);}}@Overrideprotected void measureChild(View child, int parentWidthMeasureSpec,int parentHeightMeasureSpec) {final LayoutParams lp = child.getLayoutParams();//获取子控件的宽高约束规则final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,getPaddingLeft() + getPaddingRight(), lp.width);final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,getPaddingLeft() + getPaddingRight(), lp.height);child.measure(childWidthMeasureSpec, childHeightMeasureSpec);}private int getDesiredHeight(int mItemHeight) {int totalHeight = getPaddingTop() + getPaddingBottom();int count = getChildCount();if (count > 0) {int row = (count - 1) / mColumnCount;totalHeight = (int) ((row + 1) * mItemHeight + (row) * mSpacing) + totalHeight;}return totalHeight;}private int getDesiredWidth(int mItemWidth) {int totalWidth = getPaddingLeft() + getPaddingRight();int count = getChildCount();if (count > 0) {if (count < mColumnCount) {totalWidth = (int) (count * mItemWidth + (count - 1) * mSpacing) + totalWidth;} else {totalWidth = (int) (count * mItemWidth + (count - 1) * mSpacing) + totalWidth;}}return totalWidth;}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {for (int i = 0; i < getChildCount(); i++) {View imageView = getChildAt(i);int column = i % mColumnCount;int row = i / mColumnCount;int left = (int) (getPaddingLeft() + column * (mSpacing + mItemWidth));int top = (int) (getPaddingTop() + row * (mSpacing + mItemHeight));imageView.layout(left, top, left + mItemWidth, top + mItemHeight);}}/*** 显示图片*/public void setImageUrls(final List<String> imageUrls) {removeAllViews();if (imageUrls==null||imageUrls.size()==0) {return;}int count = imageUrls.size();if (count == 1) {//一般在url中会有宽高 可以算出宽高比//测试url固定用的 1000:1376 // mItemAspectRatio = AppUtil.getAspectRatio(imageUrls.get(0));mItemAspectRatio = 1000/1376f;} else {mItemAspectRatio = 1;}for (int i = 0; i < imageUrls.size(); i++) {ImageView imageView = new ImageView(getContext());Glide.with(getContext()).load(imageUrls.get(i)).into(imageView);addView(imageView);//点击查看大图imageView.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {}});}}public int getItemWidth() {return mItemWidth;}public int getItemHeight() {return mItemHeight;}public int getColumnCount() {return mColumnCount;}public void setColumnCount(int columnCount) {mColumnCount = columnCount;invalidate();}public float getSpacing() {return mSpacing;}public void setSpacing(float spacing) {mSpacing = spacing;invalidate();}public float getItemAspectRatio() {return mItemAspectRatio;}public void setItemAspectRatio(float itemAspectRatio) {mItemAspectRatio = itemAspectRatio;} }
第二步:
直接让其用在布局里
<com.creditloan.money.shishi2.FriendsCircleImageLayoutandroid:id="@+id/mylist"android:layout_width="wrap_content"android:layout_height="wrap_content"> </com.creditloan.money.shishi2.FriendsCircleImageLayout>
第三步:只需给其复制即可
FriendsCircleImageLayout mylist = findViewById(R.id.mylist);List<String> list=new ArrayList<>();list.add("http://c.hiphotos.baidu.com/image/h%3D300/sign=0cc74ef9a3773912db268361c8188675/9922720e0cf3d7ca810f3732 f81fbe096a63a9fd.jpg");mylist.setImageUrls(list);
这样就完成了,如果有细节方面更改,自己来即可.
仿微信朋友圈根据图片数量不同而显示不同格式相关推荐
- 仿微信朋友圈【九宫格的实现】
仿微信朋友圈[九宫格的实现] 标签: 九宫格自定义viewgroup 2017-04-18 18:39 561人阅读 评论(0) 收藏 举报 分类: Android(25) 版权声明:本 ...
- android从九宫格全屏预览,仿微信朋友圈展示图片的九宫格图片展示控件,支持点击图片全屏预览大图...
AssNineGridView 仿微信朋友圈展示图片的九宫格图片展示控件,支持点击图片全屏预览大图(可自定义). 写在前面 这是一个九宫格控件,本来是很久之前就写好了,现在才开源出来,也是看了很多优秀 ...
- android com.mylhyl,Android 高仿微信朋友圈拍照上传功能
模仿微信朋友圈发布动态,输入文字支持文字多少高度自增,有一个最小输入框高度,输入文字有限制,不过这些都很easy! 1. photopicker的使用 这是一个支持选择多张图片,点击图片放大,图片之间 ...
- android 微信高仿,Android 高仿微信朋友圈拍照上传功能
模仿微信朋友圈发布动态,输入文字支持文字多少高度自增,有一个最小输入框高度,输入文字有限制,不过这些都很easy! 1. PhotoPicker的使用 这是一个支持选择多张图片,点击图片放大,图片之间 ...
- Android 仿微信朋友圈添加图片
github地址(欢迎下载Demo) https://github.com/zhouxu88/WXCircleAddPic 老习惯,先上图,着急用的朋友,直接带走Demo,先拿来用吧,毕竟老板催的紧, ...
- IOS仿微信朋友圈的日期处理
IOS仿微信朋友圈的日期处理 经常刷微信朋友圈的朋友,都能看到该条信息是什么发送的,有刚刚,有昨天,有xxxx年xx月xx日发送的,今天我们就探究微信内部是怎么样去做的. 对于传入的时间,一般是从服务 ...
- android 仿微信朋友圈 评论,2020年android 仿微信朋友圈 评论
2020年android 仿微信朋友圈 评论 1.如果有人问我:那些艰难的岁月你是怎么熬过来的?我想我只有一句话回答:我有一种强大的精神力量支撑着我,这种力量名字叫"想死又不敢" ...
- php仿微信朋友圈网站源码,Smobiler仿微信朋友圈的消息代码实例
这篇文章主要介绍了.Net语言Smobiler开发平台如何仿微信朋友圈的消息样式?本文为大家揭晓答案 最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xam ...
- Android仿微信朋友圈7实现点赞功能
前言: 之前一直有朋友问我点赞怎么实现?今天趁着休息时间整理出来,其实点赞的功能和用户评论差不多,都是显示一个用户列表,只不过评论有评论内容和回复评论功能.实现点赞的思路如下: 1.当用户点击点赞按钮 ...
最新文章
- CodeBlcoks插件
- win11 wsl及ubuntu设置为wsl 2
- 数据类型,隐式类型转换
- 爸爸我爱您(之十二)
- [知识图谱实战篇] 七.HTML+D3实现关系图谱搜索功能
- Delphi工具之Image Editor
- linux日志绕接,Logback配置
- PS制作棱镜化多边形照片效果
- android学习笔记---使用AsyncTask实现异步处理,内部使用线程加Handler
- python webbrowser_Python中webbrowser的用法
- android photopicker怎么修改状态栏,一个非常好看的图片选择框架LPhotoPicker,确定不来看看么...
- 应用程序的可视化图形引擎库控件Vectordraw Developer Framework
- 生活情景英语:英语国家生存英语精选
- C# sqlhelper
- 白帽黑客郭盛华:供应链枢纽需要从网络安全开始
- 基于SpringBoot的QQ邮箱登录注册
- 【人工生态系统优化算法】基于人工生态系统优化算法求解单目标优化问题附matlab代码
- 腾讯游戏客户端一面准备
- gii无法访问 yii2_Gii的CURD生成无法访问?
- 长安居大不易--用大数据分析西安租房现状
热门文章
- 三维模型3ds模型下载网站
- iOS给View添加虚线边框不显示
- flink连接redis工具类-简单好用
- ESP8266/ESP32 + MicroPython (一) 初识
- Chrome提示网页证书不安全,但没有高级、忽略选项的解决办法
- dboxShare 开源企业网盘系统v2.0.0.2011
- 软件系统三基座之一:权限管理
- PHP字符串去除首尾指定字符的trim ltrim rtrim函数
- 北醒发布长距固态激光雷达,完成B2轮融资 | CES 2019...
- 【信息安全案例】——信息内容安全(学习笔记)