端午過去不久,假期還沒享受完就急急忙忙來了工作,本來假期前想寫這遍文章的,結果一直忙的沒空,對于微信朋友圈和微博上傳圖片大家應該都很熟悉吧,微信幾乎是現在廣大男女老少的必備,我也是其中一個,,O(∩_∩)O,每天擠著地鐵刷刷微博朋友圈看看頭條,一小時就默然的過去了。接下來介紹下NineGridlayout、這個控件,相信用到的都不陌生,github 安卓社區上很多這樣的Demo,但是在用到的地方還是得根據自己的需求進行修改就好了,切入正題
首先我們來看看效果圖
jiu.png
- 自定義view 繼承 ViewGroup,在多張圖片的時候根據屏幕進行適配,單張圖片時需要自己指定圖片的寬高。
1.大家都知道自定義view的基本過程,這里就省略了,貼出主要的代碼
/** 設置圖片數據 Image是存放圖片的實體<Url、寬和高>*/
private int gap = 5; //圖片之間的間隔
private int columns; //列
private int rows; //行
private List listData;
private int totalWidth;
//構造方法
public NineGridlayout(Context context, AttributeSet attrs) { super(context, attrs); ScreenTools screenTools=ScreenTools.instance(getContext());
totalWidth=screenTools.getScreenWidth()-screenTools.dip2px(80);
}
public void setImagesData(List<Image> lists) {
if (lists == null || lists.isEmpty()) {
return;
}
//初始化子布局
generateChildrenLayout(lists.size());
//這里做一個重用view的處理
if (listData == null) {
int i = 0;
while (i < lists.size()) {
CustomImageView iv = generateImageView();
addView(iv, generateDefaultLayoutParams());
i++;
}
} else {
int oldViewCount = listData.size();
int newViewCount = lists.size();
if (oldViewCount > newViewCount) {
removeViews(newViewCount - 1, oldViewCount - newViewCount);
} else if (oldViewCount < newViewCount) {
for (int i = 0; i < newViewCount - oldViewCount; i++) {
CustomImageView iv = generateImageView();
addView(iv, generateDefaultLayoutParams());
}
}
}
listData = lists;
layoutChildrenView();
}
2.根據圖片個數確定行列數量
private void generateChildrenLayout(int length) {
if (length <= 3) {
rows = 1;
columns = length;
} else if (length <= 6) {
rows = 2;
columns = 3;
if (length == 4) {
columns = 2;
}
} else {
rows = 3;
columns = 3;
}
}
3.設置對應的索引
private int[] findPosition(int childNum) {
int[] position = new int[2];
for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { if ((i * columns + j) == childNum) { position[0] = i;//行 position[1] = j;//列 break; } } } return position;}
4.計算子view的位置
private void layoutChildrenView(){
int childrenCount = listData.size();
int singleWidth = (totalWidth - gap * (3 - 1)) / 3;
int singleHeight = singleWidth;
//根據子view數量確定高度
ViewGroup.LayoutParams params = getLayoutParams();
params.height = singleHeight * rows + gap * (rows - 1);
setLayoutParams(params);
for (int i = 0; i < childrenCount; i++) {
CustomImageView childrenView = (CustomImageView) getChildAt(i);
childrenView.setImageUrl(((Image) listData.get(i)).getUrl());
int[] position = findPosition(i);
int left = (singleWidth + gap) * position[1];
int top = (singleHeight + gap) * position[0];
int right = left + singleWidth;
int bottom = top + singleHeight;
childrenView.layout(left, top, right, bottom);
}
5.在布局中使用
<com.lukey.ninegridlayout.NineGridlayout
android:id="@+id/nine_gd_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp"/>
6.在MainActivity 設置數據的時候 9條朋友圈對應就9張圖片, images.length是圖片數組的數 據源,demo上的圖片來源于網絡
for (int i = 0; i < images.length; i++) { ArrayList<Image> itemList = new ArrayList<>(); for (int j = 0; j <= i; j++) { itemList.add(new Image(images[j][0], Integer.parseInt(images[j][1]), Integer.parseInt(images[j][2]))); } imagesList.add(itemList);}
- 主要的代碼自定義view就是這么多了,在調用的時候咱們還得用圖片進行賦值自定義的圖片,CustomImageView是自定義的圖片view,大家也可以用fresco,或者ImageView,當然了這個沒有定制性的規定只是實現出這個效果就O了。需要完整demo的朋友可以留言
- 下面是幾個反雞湯語錄,有興趣的朋友可以看看,平時喜歡段子和優雅的心靈雞湯,看看就好
有時候你不逼自己一把,你就不知道你還有把事情搞砸的本事。
掉落枯井的王老漢在熱心村民們的幫助之下總算適應了井底生活。
真正努力過的人,就會明白天賦的重要。
上帝是公平的,給了一張丑臉,當然也會給你一個矮的身高來搭配。
還是要努力的,不然有時候不努力一把你就不知道什么叫絕望。
以前車馬信件都很慢,書信很遠,一生只夠愛一個人,但……能納很多妾……
為什么別人看你一眼就覺得你是學生?不是因為你長得小,而是因為你穿的土。
朋友遭遇挫折后,如果他茶飯不思,不要太擔心,這說明他還在思考問題。如果他暴飲暴食,那就要小心了,這種破罐子破摔的人,跳樓都是小事,就怕他偷你零食。
沉默,沉默是今晚的康橋,趙州橋,鄭板橋。
一路上有你,苦一點也愿意。苦多了免談。
- 努力真的有回報,如果暫時沒有,只是努力不夠。
- Github源碼