日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > 一百行代碼實現微信朋友圈九宮格圖片顯示

一百行代碼實現微信朋友圈九宮格圖片顯示

來源:程序員人生   發布時間: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的高度來肯定的.


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

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

  5.         //根據子view數量肯定高度
  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.     }
復制代碼

   添加1個設置圖片資源的接口,1般情況下我們都是用在listview來顯示數據,而數據都是封裝好的,這里提供1個Image封裝類,接口和封裝類代碼以下:  
  1.     public void setImagesData(List<Image> lists) {
  2.         if (lists == null || lists.isEmpty()) {
  3.             return;
  4.         }
  5.         //初始化布局
  6.         generateChildrenLayout(lists.size());
  7.         //這里做1個重用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.     }
復制代碼

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. }
復制代碼




在添加數據的時候,我們要根據圖片的個數來肯定具體的布局情況,這個函數就是generateChildrenLayout(),實現以下:
  1.     /**
  2.      * 根據圖片個數肯定行列數量
  3.      * 對應關系以下
  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.     }
復制代碼
這些,就是NineGridLayout的核心代碼了,是否是很簡單,全部類的源碼以下:
  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.         //根據子view數量肯定高度
  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. 生活不易,碼農辛苦
    如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
    程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 国产精品一区二区久久久 | 在线免费看黄色 | 神马久久久久 | 国产中文字幕在线 | 黄色毛片免费观看 | 精品三级国产 | 久久久国产一区 | 热久久91 | 欧美日韩在线视频免费 | 日批视频免费观看 | 亚洲伊人网站 | 国产99在线 | 欧美 | av九九| 在线不卡免费视频 | 国产精品一区二区三 | 国产乱妇4p交换乱免费视频 | 国产伦精品一区二区三区视频孕妇 | 国产伦精品一区二区三区免费 | 国产精品成人一区二区网站软件 | 欧美性猛交xxxx黑人交 | 日韩av电影网 | 亚洲精品久久久蜜桃 | 国家一级毛片 | 福利网站在线观看 | 美女视频黄是免费的 | 国产成人99久久亚洲综合精品 | 国产日韩欧美一区二区 | 亚洲国产精品久久 | 91视频国产一区 | 精品久久久999 | 久久久亚洲精品视频 | 午夜免费网站 | 欧美国产日韩一区二区三区 | 色五月婷婷成人网 | 日本精品久久 | 黄色av大全| 久久天天综合 | 福利视频一区二区三区 | 欧美国产一区二区 | 国产麻豆精品在线观看 | 99在线看|