

FlexboxLayout实现不规则图片墙
source link: https://mingdroid.github.io/2016/11/08/FlexboxLayout%E5%AE%9E%E7%8E%B0%E4%B8%8D%E8%A7%84%E5%88%99%E5%9B%BE%E7%89%87%E5%A2%99/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

前提:服务器返回图片布局形式,图片大小。如下图的布局形式为[1,3,2,1]

1. 了解FlexboxLayout功能
https://github.com/google/flexbox-layout
https://www.oschina.net/news/73442/google-flexbox-layout
FlexboxLayout支持的属性:
FlexboxLayout的child view可设置的属性,略。
2. 解决方案
使用以下属性,同时设置好ImageView的宽高即可。
方法1:
app:flexDirection=“row”
app:flexWrap=“wrap”
app:justifyContent=“space_between”
app:showDividerHorizontal=“middle”
app:dividerDrawableHorizontal="@drawable/shape_flexbox_divider"
设置详解:
- 主轴方向为行
- 在一行内的对齐方式是图片之间置空
- 显示水平分割线,在每行之间显示。
app:flexDirection=“row”
app:flexWrap=“wrap”
app:showDivider=“middle”
app:dividerDrawable="@drawable/shape_flexbox_divider"
设置详解:
- 主轴方向为行
- 同时显示水平和垂直的分割线,在图片之间显示,在行之间显示。
注意:由于使用的是dp单位,在计算时有除法可能出现误差,一定要计算精确,要不然会出现意外的换行(我用方法2就出现了意外换行,计算不精确)
FlexboxLayout的xml配置:
1
2
3
4
5
6
7
8
9
10
11
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:flexDirection="row"
app:flexWrap="wrap"
app:justifyContent="space_between"
app:showDividerHorizontal="middle"
app:dividerDrawableHorizontal="@drawable/shape_flexbox_divider"
/>
分割线资源文件配置:
1
2
3
4
5
6
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="4dp"
android:height="4dp"/>
<solid android:color="@android:color/white"/>
</shape>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
private ArrayList<Info> photos; //图片信息,包含宽高
private ArrayList<ImageView> viewCache; //缓存ImageView
private int[] layout = {1, 3, 2, 1}; //布局形式
private int dividerWidth; //分割线大小
private WIDTH; //FlexboxLayout的宽度
private void setPhotoContent() {
flexboxLayout.removeAllViews();
createPhotoView(photos.size());
int position = 0;
for (int i = 0; i < layout.length; i++) {
int w = (WIDTH -(layout[i] - 1) * dividerWidth) / layout[i];
int h = w * info.getHeight() / info.getWidth();
for (int j = 0; j < layout[i]; j++) {
setImageViewSize(viewCache(position), w, h);
//其他设置,如加载图片
setUri(viewCache.get(position), photos.get(position));
flexboxLayout.addView(view);
position++;
}
}
}
private void setImageViewSize(ImageView view, int w, int h) {
}
private void setUri(ImageView view, Info info) {
}
private void createPhotoView(int count) {
//创建所需数量ImageView, 图片数量太多的话,该方案不适用
while (count > viewCache.size()) {
viewCache.add(new ImageView());
}
}
Recommend
-
178
作者简介 敏岳 蚂蚁金服数据前端 在浏览器中,任意的二维平面图形均可以通过path路径的形式描述。然后底层api 直接静态绘制出来。但是如果想动态的绘制路径,浏览器是没有直接支持方式的。 本文就是解决这个问题, 为浏览器补全这个功能,让静态的路径能方便的动态...
-
102
README.md FlexBoxLayout
-
45
-
106
README.md
-
62
RelativeLayout、LinearLayout等常用布局相信大家早已耳熟能详,今天给大家介绍一款新的布局方式「弹性布局」--FlexboxLayout。Flex是FlexibleBox的缩写,意为「弹性布局」,在前端css样式中应用甚为广泛,之前做过React-Native和微信小程序,页面布局大多使用flex...
-
10
第195期:FlexboxLayout 帮助您完成聪明的 UI 布局 1、FlexboxLayout 帮助您完成聪明的 UI 布局 去年 Google 官方 在 Google I/O 上发布了 ConstraintLayout,在构建复杂布局的同时能够让视图...
-
12
2018-03-03 • UGUI中几种不规则按钮的实现方式 UGUI中的按钮默认是矩形的,若要实现非矩形按钮该怎么做呢?比如这样的按钮:
-
18
Android 不规则图像填充 小玩着色游戏
-
12
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/45954255; 本文出自:【张鸿洋...
-
9
PixiJS+自定义Shader实现不规则图片扫光效果 - 一行代码一行泪 - OSCHINA - 中文开源技术交流社区 用代码保护...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK