多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > php開(kāi)源 > 綜合技術(shù) > 一百行代碼實(shí)現(xiàn)微信朋友圈九宮格圖片顯示

一百行代碼實(shí)現(xiàn)微信朋友圈九宮格圖片顯示

來(lái)源:程序員人生   發(fā)布時(shí)間:2015-05-14 08:44:43 閱讀次數(shù):9871次
前言
    很多時(shí)候我們都在刷微博或微信朋友圈的時(shí)候都會(huì)看到很多圖片,而這些圖片的顯示跟我們平時(shí)很多控件的顯示方式都不1樣,而且,當(dāng)我們仔細(xì)去視察后就會(huì)發(fā)現(xiàn),他加載的圖片都是根據(jù)圖片數(shù)量動(dòng)態(tài)加載的,根據(jù)不同的圖片數(shù)量來(lái)用不同的布局顯示

當(dāng)圖片是4張的時(shí)候,就會(huì)構(gòu)成1個(gè)2x2的正方形,除1張的情況,另外的都是依照9宮格的方式顯示和排列圖片的。那末這類布局是怎樣實(shí)現(xiàn)的呢,1開(kāi)始,好多人都可能認(rèn)為用原生的GridView就可以弄掂,但是,卻有幾種特殊的情況是GridView解決不了的,例如4張圖片的情況,或1張,其實(shí)也能夠根據(jù)圖片的數(shù)量然后用幾個(gè)不同布局的GridView來(lái)實(shí)現(xiàn),不過(guò)那樣的話就復(fù)雜很多了。而且處理起來(lái)很麻煩,其實(shí),大部份的實(shí)現(xiàn)都是通過(guò)自定義ViewGroup來(lái)實(shí)現(xiàn)的,通過(guò)代碼編寫來(lái)設(shè)定childrenView的layout來(lái)實(shí)現(xiàn)這類布局,而NineGridView控件就是這么1個(gè)東西,代碼其實(shí)很簡(jiǎn)單,100行就夠了。
代碼編寫                         
     先自定義1個(gè)View集成ViewGroup,編輯器會(huì)提示你實(shí)現(xiàn)OnLayout方法,實(shí)現(xiàn)之,這里我們動(dòng)態(tài)的添加的話其實(shí)不用到OnLayout方法,自定義1個(gè)layoutChildrenView()用來(lái)為子view設(shè)定位置就好了,該方法的實(shí)現(xiàn)以下:
        這代碼里面在調(diào)用子view的layout方法的同時(shí)設(shè)定了本身ViewGroup的高度大小,由于NineGridView的高度是要根據(jù)子View的高度來(lái)肯定的.


  1.     private void layoutChildrenView(){
  2.         int childrenCount = listData.size();

  3.         int singleWidth = (totalWidth - gap * (3 - 1)) / 3;
  4.         int singleHeight = singleWidth;

  5.         //根據(jù)子view數(shù)量肯定高度
  6.         ViewGroup.LayoutParams params = getLayoutParams();
  7.         params.height = singleHeight * rows + gap * (rows - 1);
  8.         setLayoutParams(params);

  9.         for (int i = 0; i < childrenCount; i++) {
  10.             CustomImageView childrenView = (CustomImageView) getChildAt(i);
  11.             childrenView.setImageUrl(((Image) listData.get(i)).getUrl());
  12.             int[] position = findPosition(i);
  13.             int left = (singleWidth + gap) * position[1];
  14.             int top = (singleHeight + gap) * position[0];
  15.             int right = left + singleWidth;
  16.             int bottom = top + singleHeight;

  17.             childrenView.layout(left, top, right, bottom);
  18.         }

  19.     }
復(fù)制代碼

   添加1個(gè)設(shè)置圖片資源的接口,1般情況下我們都是用在listview來(lái)顯示數(shù)據(jù),而數(shù)據(jù)都是封裝好的,這里提供1個(gè)Image封裝類,接口和封裝類代碼以下:  
  1.     public void setImagesData(List<Image> lists) {
  2.         if (lists == null || lists.isEmpty()) {
  3.             return;
  4.         }
  5.         //初始化布局
  6.         generateChildrenLayout(lists.size());
  7.         //這里做1個(gè)重用view的處理
  8.         if (listData == null) {
  9.             int i = 0;
  10.             while (i < lists.size()) {
  11.                 CustomImageView iv = generateImageView();
  12.                 addView(iv,generateDefaultLayoutParams());
  13.                 i++;
  14.             }
  15.         } else {
  16.             int oldViewCount = listData.size();
  17.             int newViewCount = lists.size();
  18.             if (oldViewCount > newViewCount) {
  19.                 removeViews(newViewCount - 1, oldViewCount - newViewCount);
  20.             } else if (oldViewCount < newViewCount) {
  21.                 for (int i = 0; i < newViewCount - oldViewCount; i++) {
  22.                     CustomImageView iv = generateImageView();
  23.                     addView(iv,generateDefaultLayoutParams());
  24.                 }
  25.             }
  26.         }
  27.         listData = lists;
  28.         layoutChildrenView();
  29.     }
復(fù)制代碼

Image封裝類:

  1. public class Image {
  2.     private String url;
  3.     private int width;
  4.     private int height;

  5.     public Image(String url, int width, int height) {
  6.         this.url = url;
  7.         this.width = width;
  8.         this.height = height;
  9.         L.i(toString());
  10.     }

  11.     public String getUrl() {
  12.         return url;
  13.     }

  14.     public void setUrl(String url) {
  15.         this.url = url;
  16.     }

  17.     public int getWidth() {
  18.         return width;
  19.     }

  20.     public void setWidth(int width) {
  21.         this.width = width;
  22.     }

  23.     public int getHeight() {
  24.         return height;
  25.     }

  26.     public void setHeight(int height) {
  27.         this.height = height;
  28.     }

  29.     @Override
  30.     public String toString() {

  31.         return "image---->>url="+url+"width="+width+"height"+height;
  32.     }
  33. }
復(fù)制代碼




在添加數(shù)據(jù)的時(shí)候,我們要根據(jù)圖片的個(gè)數(shù)來(lái)肯定具體的布局情況,這個(gè)函數(shù)就是generateChildrenLayout(),實(shí)現(xiàn)以下:
  1.     /**
  2.      * 根據(jù)圖片個(gè)數(shù)肯定行列數(shù)量
  3.      * 對(duì)應(yīng)關(guān)系以下
  4.      * num        row        column
  5.      * 1           1        1
  6.      * 2           1        2
  7.      * 3           1        3
  8.      * 4           2        2
  9.      * 5           2        3
  10.      * 6           2        3
  11.      * 7           3        3
  12.      * 8           3        3
  13.      * 9           3        3
  14.      *
  15.      * @param length
  16.      */
  17.     private void generateChildrenLayout(int length) {
  18.         if (length <= 3) {
  19.             rows = 1;
  20.             columns = length;
  21.         } else if (length <= 6) {
  22.             rows = 2;
  23.             columns = 3;
  24.             if (length == 4) {
  25.                 columns = 2;
  26.             }
  27.         } else {
  28.             rows = 3;
  29.             columns = 3;
  30.         }
  31.     }
復(fù)制代碼
這些,就是NineGridLayout的核心代碼了,是否是很簡(jiǎn)單,全部類的源碼以下:
  1. package com.weixinninegridlayout;

  2. import android.content.Context;
  3. import android.graphics.Color;
  4. import android.graphics.drawable.ColorDrawable;
  5. import android.util.AttributeSet;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.ImageView;

  9. import java.util.List;


  10. /**
  11. * Created by Pan_ on 2015/2/2.
  12. */
  13. public class NineGridlayout extends ViewGroup {

  14.     /**
  15.      * 圖片之間的間隔
  16.      */
  17.     private int gap = 5;
  18.     private int columns;//
  19.     private int rows;//
  20.     private List listData;
  21.     private int totalWidth;

  22.     public NineGridlayout(Context context) {
  23.         super(context);
  24.     }

  25.     public NineGridlayout(Context context, AttributeSet attrs) {
  26.         super(context, attrs);
  27.         ScreenTools screenTools=ScreenTools.instance(getContext());
  28.         totalWidth=screenTools.getScreenWidth()-screenTools.dip2px(80);
  29.     }

  30.     @Override
  31.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  32.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  33.     }

  34.     @Override
  35.     protected void onLayout(boolean changed, int l, int t, int r, int b) {

  36.     }
  37.     private void layoutChildrenView(){
  38.         int childrenCount = listData.size();

  39.         int singleWidth = (totalWidth - gap * (3 - 1)) / 3;
  40.         int singleHeight = singleWidth;

  41.         //根據(jù)子view數(shù)量肯定高度
  42.         ViewGroup.LayoutParams params = getLayoutParams();
  43.         params.height = singleHeight * rows + gap * (rows - 1);
  44.         setLayoutParams(params);

  45.         for (int i = 0; i < childrenCount; i++) {
  46.             CustomImageView childrenView = (CustomImageView) getChildAt(i);
  47.             childrenView.setImageUrl(((Image) listData.get(i)).getUrl());
  48.             int[] position = findPosition(i);
  49. 生活不易,碼農(nóng)辛苦
    如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
    程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 手机看片福利国产 | 天堂网在线网站成人午夜网站 | www.久久精品视频 | 亚洲免费观看视频 | 国内自拍在线视频高清 | 亚洲性生活网站 | 最新国产在线视频 | 欧美日本黄色片 | ww视频在线观看 | 国产日韩欧美在线一区二区三区 | 欧美一欧美一区二三区性 | 色老头久久久久久久久久 | 久久久久久久久久久久久久久久久久久久 | 日本护士毛片在线视频 | 国产a久久精品一区二区三区 | 欧美性受xxxx喷水性欧洲 | 噜噜噜噜私人影院 | 欧美日韩一级片在线观看 | 亚洲精品www久久久久久久软件 | www.亚洲精品.com | 久久亚洲不卡一区二区 | 国产日韩精品视频一区二区三区 | 精品中文字幕不卡在线视频 | 亚洲社区 | 黄色网址在线看 | free日韩性公交车上xxhd | 国产码欧美日韩高清综合一区 | 国产精品久久久久久免费 | 一本久道久久综合婷婷五 | 欧美性猛交乱大交xxxx | 久久久无码精品亚洲日韩按摩 | 亚洲成人黄色在线 | 欧美综合自拍亚洲综合 | 中文字幕精品一区 | 日本精品一区二区在线播放 | 最近更新中文字幕免费版 | 欧美性受xxxx喷水大胸 | 一级毛片国产真人永久在线 | 波多野结衣免费一区二区三区香蕉 | h视频无遮挡免费网站 | 欧美一级特黄aa大片视频 |