简介

这样的点赞列表怎么样?之前做社区的时候也有类似的点赞列表,但是没有这样重叠,一个小小的改变,个人感觉逼格提高不少。

这个很有规则,就是后一个头像会覆盖一部分到前一个头像上,头像多了就像一串糖葫芦了。

这个实现起来不难,自定义ViewGroup,关键重写onLayout方法。

关于自定义控件的基础知识可以看一看这个,整理的很详细: https://github.com/GcsSloop/AndroidNote

实现

自定义属性

属性名

说明

默认值

vertivalSpace

行距

4dp

pileWidth

重叠宽度

10dp

onMeasure方法,每行的宽度不再是child的宽度和了,而是要减掉重叠部分的宽度和

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);

int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);

int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);

int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

//AT_MOST

int width = 0;

int height = 0;

int rawWidth = 0;//当前行总宽度

int rawHeight = 0;// 当前行高

int rowIndex = 0;//当前行位置

int count = getChildCount();

for (int i = 0; i < count; i++) {

View child = getChildAt(i);

if(child.getVisibility() == GONE){

if(i == count - 1){

//最后一个child

height += rawHeight;

width = Math.max(width, rawWidth);

}

continue;

}

//这里调用measureChildWithMargins 而不是measureChild

measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);

MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;

int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;

if(rawWidth + childWidth - (rowIndex > 0 ? pileWidth : 0)> widthSpecSize - getPaddingLeft() - getPaddingRight()){

//换行

width = Math.max(width, rawWidth);

rawWidth = childWidth;

height += rawHeight + vertivalSpace;

rawHeight = childHeight;

rowIndex = 0;

} else {

rawWidth += childWidth;

if(rowIndex > 0){

rawWidth -= pileWidth;

}

rawHeight = Math.max(rawHeight, childHeight);

}

if(i == count - 1){

width = Math.max(rawWidth, width);

height += rawHeight;

}

rowIndex++;

}

setMeasuredDimension(

widthSpecMode == MeasureSpec.EXACTLY ? widthSpecSize : width + getPaddingLeft() + getPaddingRight(),

heightSpecMode == MeasureSpec.EXACTLY ? heightSpecSize : height + getPaddingTop() + getPaddingBottom()

);

}

onLayout 每一行,第一个正常放,之后的重叠放

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

int viewWidth = r - l;

int leftOffset = getPaddingLeft();

int topOffset = getPaddingTop();

int rowMaxHeight = 0;

int rowIndex = 0;//当前行位置

View childView;

for( int w = 0, count = getChildCount(); w < count; w++ ){

childView = getChildAt(w);

if(childView.getVisibility() == GONE) continue;

MarginLayoutParams lp = (MarginLayoutParams) childView.getLayoutParams();

// 如果加上当前子View的宽度后超过了ViewGroup的宽度,就换行

int occupyWidth = lp.leftMargin + childView.getMeasuredWidth() + lp.rightMargin;

if(leftOffset + occupyWidth + getPaddingRight() > viewWidth){

leftOffset = getPaddingLeft(); // 回到最左边

topOffset += rowMaxHeight + vertivalSpace; // 换行

rowMaxHeight = 0;

rowIndex = 0;

}

int left = leftOffset + lp.leftMargin;

int top = topOffset + lp.topMargin;

int right = leftOffset+ lp.leftMargin + childView.getMeasuredWidth();

int bottom = topOffset + lp.topMargin + childView.getMeasuredHeight();

childView.layout(left, top, right, bottom);

// 横向偏移

leftOffset += occupyWidth;

// 试图更新本行最高View的高度

int occupyHeight = lp.topMargin + childView.getMeasuredHeight() + lp.bottomMargin;

if(rowIndex != count - 1){

leftOffset -= pileWidth;//这里控制重叠位置

}

rowMaxHeight = Math.max(rowMaxHeight, occupyHeight);

rowIndex++;

}

}

效果图

因为这个一般只会显示一行,所以暂时没有通过setAdapter方式去设置数据源。

下载

https://github.com/LineChen/PileLayout

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

本文标题: Android自定义ViewGroup实现堆叠头像的点赞Layout

本文地址: http://www.cppcns.com/ruanjian/android/207676.html

头像叠加android_Android自定义ViewGroup实现堆叠头像的点赞Layout相关推荐

  1. 头像叠加android_Android开发头像挨着叠加效果

    一.效果图 头像挨着.png 二.思路分析 叠加肯定用到FrameLayout,因为数量不一定,就需要动态addView.然后 headParams.leftMargin来设置偏移量 三.案例关键代码 ...

  2. Android仿拼多多拼团堆叠头像

    序言 做电商的都知道,作为商品的一种售卖方式,拼团是是提供商品售卖的一种及时有效的方式,而在拼团市场中,拼多多无疑是做的最好的一家.于是,研究拼多多的售卖方式之后,我们的产品也开始了这方面的开发.本文 ...

  3. android 清空canvas部分内容_Android自定义View实现圆形头像效果

    在我们的APP中通常会遇到,展示圆形头像的需求,一般通过Glide就能实现,但是让我们做一个圆形头像,如果让我们自定义实现这种效果,该怎样做呢? 好,接下来本文通过三种方式来实现这种效果! 注意:这是 ...

  4. Android自定义ViewGroup实现朋友圈九宫格控件

    在我们的实际应用中,经常需要用到自定义控件,比如自定义圆形头像,自定义计步器等等,这篇文章主要给大家介绍了关于Android自定义ViewGroup实现朋友圈九宫格控件的相关资料,需要的朋友可以参考下 ...

  5. android 自定义viewgroup onmeasure,一篇文章搞懂Android 自定义Viewgroup的难点

    本文的目的 目的在于教会大家到底如何自定义viewgroup,自定义布局和自定义测量到底如何写.很多网上随便搜搜的概念和流程图这里不再过多描述了,建议大家看本文之前,先看看基本的自定义viewgrou ...

  6. android 自定义flowlayout,Android 自定义ViewGroup之实现FlowLayout-标签流容器

    本篇文章讲的是Android 自定义ViewGroup之实现标签流式布局-FlowLayout,开发中我们会经常需要实现类似于热门标签等自动换行的流式布局的功能,网上也有很多这样的FlowLayout ...

  7. Android 自定义ViewGroup 实战篇 - 实现FlowLayout

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38352503 ,本文出自[张鸿洋的博客] 1.概述 上一篇已经基本给大家介绍了如 ...

  8. php更换wordpress用户头像,WordPress如何添加用户自定义上传头像功能

    使用WordPress建站的朋友应该知道,WordPress本身是没有上传自定义头像功能的,如果要更换头像,步骤是非常麻烦的. 而在我们开发一款WordPress主题中,特别是多用户的主题,让注册用户 ...

  9. Android 自定义ViewGroup之实现FlowLayout-标签流容器

    本篇文章讲的是Android 自定义ViewGroup之实现标签流式布局-FlowLayout,开发中我们会经常需要实现类似于热门标签等自动换行的流式布局的功能,网上也有很多这样的FlowLayout ...

最新文章

  1. html基础实验的实验原理,html网页设计实验报告.doc
  2. Java反射是什么?看这篇绝对会了!
  3. RS232与RS485的功能与区别
  4. 04-程序计数器(PC计数器)
  5. 内存管理简介之Buddy算法和slab分配
  6. mysql和oracle 开源_MySQL和oracle比较
  7. RAC环境创建本地数据文件的解决方法
  8. 香港学计算机,香港求学计算机专业集锦
  9. Vue指令v-show和v-if的区别
  10. 关于前端程序员写前端用什么框架更好?
  11. 举例计算机的发展状况,计算机的发展过程教学设计.doc
  12. 有效值/峰-峰值/幅值/瞬时值
  13. 实验二 单管交流放大电路
  14. rgb 接口lcd 驱动调试
  15. 如何制作一寸。二寸。六寸照片 多学点,以后自己也可以弄哦
  16. 在Python里,用股票案例讲描述性统计分析方法(内容来自我的书)
  17. Spark的spark-*和blockmgr-*目录里是什东西,怎么来的
  18. 现在好的微博营销技巧都有哪些呢?
  19. 下载美拍视频 python
  20. 使用冰封重装系统遇到的问题及解决方案

热门文章

  1. 使用淘宝助理要压缩一下数据库用的是sqlite作数据库的,
  2. frontpage css,如何在frontpage中定义css样式.docx
  3. 三万字速通JavaScript
  4. 动力电池系统介绍(七)——上下电管理
  5. 飞狐交易师目录结构、文件信息详解
  6. css如何载入多种字体,在css中包含多种字体的正确方法
  7. 开始自学SAP以及学习路线
  8. 11对战平台在wayos进程分线下下载地图慢的解决
  9. Hexo提交搜索引擎收录-Baidu收录、Google收录
  10. Bugku部分密码题以及misc题