一百行代碼實現微信朋友圈九宮格圖片顯示
來源:程序員人生 發布時間:2015-05-14 08:44:43 閱讀次數:9887次
前言
很多時候我們都在刷微博或微信朋友圈的時候都會看到很多圖片,而這些圖片的顯示跟我們平時很多控件的顯示方式都不1樣,而且,當我們仔細去視察后就會發現,他加載的圖片都是根據圖片數量動態加載的,根據不同的圖片數量來用不同的布局顯示
當圖片是4張的時候,就會構成1個2x2的正方形,除1張的情況,另外的都是依照9宮格的方式顯示和排列圖片的。那末這類布局是怎樣實現的呢,1開始,好多人都可能認為用原生的GridView就可以弄掂,但是,卻有幾種特殊的情況是GridView解決不了的,例如4張圖片的情況,或1張,其實也能夠根據圖片的數量然后用幾個不同布局的GridView來實現,不過那樣的話就復雜很多了。而且處理起來很麻煩,其實,大部份的實現都是通過自定義ViewGroup來實現的,通過代碼編寫來設定childrenView的layout來實現這類布局,而NineGridView控件就是這么1個東西,代碼其實很簡單,100行就夠了。
代碼編寫
先自定義1個View集成ViewGroup,編輯器會提示你實現OnLayout方法,實現之,這里我們動態的添加的話其實不用到OnLayout方法,自定義1個layoutChildrenView()用來為子view設定位置就好了,該方法的實現以下:
這代碼里面在調用子view的layout方法的同時設定了本身ViewGroup的高度大小,由于NineGridView的高度是要根據子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);
-
}
-
-
}
復制代碼
添加1個設置圖片資源的接口,1般情況下我們都是用在listview來顯示數據,而數據都是封裝好的,這里提供1個Image封裝類,接口和封裝類代碼以下:
-
public void setImagesData(List<Image> lists) {
-
if (lists == null || lists.isEmpty()) {
-
return;
-
}
-
//初始化布局
-
generateChildrenLayout(lists.size());
-
//這里做1個重用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();
-
}
復制代碼
Image封裝類:
-
-
public class Image {
-
private String url;
-
private int width;
-
private int height;
-
-
public Image(String url, int width, int height) {
-
this.url = url;
-
this.width = width;
-
this.height = height;
-
L.i(toString());
-
}
-
-
public String getUrl() {
-
return url;
-
}
-
-
public void setUrl(String url) {
-
this.url = url;
-
}
-
-
public int getWidth() {
-
return width;
-
}
-
-
public void setWidth(int width) {
-
this.width = width;
-
}
-
-
public int getHeight() {
-
return height;
-
}
-
-
public void setHeight(int height) {
-
this.height = height;
-
}
-
-
@Override
-
public String toString() {
-
-
return "image---->>url="+url+"width="+width+"height"+height;
-
}
-
}
-
復制代碼
在添加數據的時候,我們要根據圖片的個數來肯定具體的布局情況,這個函數就是generateChildrenLayout(),實現以下:
-
/**
-
* 根據圖片個數肯定行列數量
-
* 對應關系以下
-
* num row column
-
* 1 1 1
-
* 2 1 2
-
* 3 1 3
-
* 4 2 2
-
* 5 2 3
-
* 6 2 3
-
* 7 3 3
-
* 8 3 3
-
* 9 3 3
-
*
-
* @param length
-
*/
-
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;
-
}
-
}
復制代碼
這些,就是NineGridLayout的核心代碼了,是否是很簡單,全部類的源碼以下:
-
package com.weixinninegridlayout;
-
-
import android.content.Context;
-
import android.graphics.Color;
-
import android.graphics.drawable.ColorDrawable;
-
import android.util.AttributeSet;
-
import android.view.View;
-
import android.view.ViewGroup;
-
import android.widget.ImageView;
-
-
import java.util.List;
-
-
-
/**
-
* Created by Pan_ on 2015/2/2.
-
*/
-
public class NineGridlayout extends ViewGroup {
-
-
/**
-
* 圖片之間的間隔
-
*/
-
private int gap = 5;
-
private int columns;//
-
private int rows;//
-
private List listData;
-
private int totalWidth;
-
-
public NineGridlayout(Context context) {
-
super(context);
-
}
-
-
public NineGridlayout(Context context, AttributeSet attrs) {
-
super(context, attrs);
-
ScreenTools screenTools=ScreenTools.instance(getContext());
-
totalWidth=screenTools.getScreenWidth()-screenTools.dip2px(80);
-
}
-
-
@Override
-
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
-
}
-
-
@Override
-
protected void onLayout(boolean changed, int l, int t, int r, int b) {
-
-
}
-
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);
-
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
------分隔線----------------------------
------分隔線----------------------------