Android GridView仿微信朋友圈顯示圖片_Android

來源:互聯網
上載者:User

最近項目要求上傳多圖並且多圖顯示,而且要規則的顯示,就像微信朋友圈的圖片顯示一樣。
利用GridView再適合不過了,GridView可以動態載入圖片的數量,而且還比較規律,下面說一下自己的思路:

  • 1.擷取網狀圖片
  • 2.初始化gridview,自訂配接器
  • 3.根據圖片數量設定gridview的列數
  • 4.更新適配器

下面貼上部分源碼並給大家解析一下
一、首先是GridView的item

<com.view.SquareLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:orientation="vertical" >  <ImageView   android:id="@+id/item_grida_image"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:scaleType="fitXY"   android:layout_margin="@dimen/tinyest_space">  </ImageView> </com.view.SquareLayout> 

這裡的SquareLayout布局是自訂的下面會給大家詳細講解。
子項中是一個正方形布局裡面嵌套著圖片

二、接下來自訂配接器
因為項目需求不同,自己定義的適配器和平時用的不太一樣,這裡就不貼源碼了。大體上也是將圖片下載到本地,用Imageloader載入,不過我這裡有上傳失敗的和建立的,所以不太一樣。

三、最後在用到的Activity中設定

noScrollgridview = (GridView) findViewById(R.id.noScrollgridview);   noScrollgridview.setNumColumns(3); //預設設定在3列圖片   //上傳成功傳值給adapter   picAdapter = new PictureAdapter(this, 1, appItem_file);   noScrollgridview.setAdapter(picAdapter); //根據圖片數量設定圖片的列   int size = appItemFile.getFiles().split(",").length;   if (size==1){    noScrollgridview.setNumColumns(1);   }   else if (size==2){    noScrollgridview.setNumColumns(2);   }   else if (size>2){    noScrollgridview.setNumColumns(3);   }   picAdapter.notifyDataSetChanged(); 

預設設定GridView的列數為3,根據圖片的數量動態設定列數。

最後貼上SquareLayout的源碼解析一下

/**  * 方形布局  */ public class SquareLayout extends RelativeLayout {  public SquareLayout(Context context, AttributeSet attrs, int defStyle) {   super(context, attrs, defStyle);  }   public SquareLayout(Context context, AttributeSet attrs) {   super(context, attrs);  }   public SquareLayout(Context context) {   super(context);  }   @SuppressWarnings("unused")  @Override  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {   // For simple implementation, or internal size is always 0.   // We depend on the container to specify the layout size of   // our view. We can't really know what it is since we will be   // adding and removing different arbitrary views and do not   // want the layout to change as this happens.   setMeasuredDimension(getDefaultSize(0, widthMeasureSpec),     getDefaultSize(0, heightMeasureSpec));    // Children are just made to fill our space.   int childWidthSize = getMeasuredWidth();   int childHeightSize = getMeasuredHeight();   // 高度和寬度一樣   heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(     childWidthSize, MeasureSpec.EXACTLY);   super.onMeasure(widthMeasureSpec, heightMeasureSpec);  } } 

這裡主要重寫了onMeasure()方法,設定了高寬,需要注意的是在用SquareLayout的時候要設定它的高寬都是match_parent。這樣就可以填滿GridView的每一項了。
接下來貼圖給大家看:

ImgeView的scaleType的屬性如果設定FitXY就會充滿方形布局,如果center就會置中顯示
詳細說一下吧:
1)center:保持原圖的大小,顯示在ImageView的中心。當原圖的size大於ImageView的size,超過部分裁剪處理。
2)centerCrop:以填滿整個ImageView為目的,將原圖的中心對準ImageView的中心,等比例放大原圖,直到填滿ImageView為止(指的是ImageView的寬和高都要填滿),原圖超過ImageView的部分作裁剪處理。
3)centerInside:以原圖完全顯示為目的,將圖片的內容完整置中顯示,通過按比例縮小原圖的size寬(高)等於或小於ImageView的寬(高)。如果原圖的size本身就小於ImageView的size,則原圖的size不作任何處理,置中顯示在ImageView。
4)matrix:不改變原圖的大小,從ImageView的左上方開始繪製原圖,原圖超過ImageView的部分作裁剪處理
5)fitCenter:把原圖按比例擴大或縮小到ImageView的ImageView的高度,置中顯示
6)fitEnd:把原圖按比例擴大(縮小)到ImageView的高度,顯示在ImageView的下部分位置
7)fitStart:把原圖按比例擴大(縮小)到ImageView的高度,顯示在ImageView的上部分位置
8)fitXY:把原圖按照指定的大小在View中顯示,展開顯示圖片,不保持原比例,填滿ImageView.

本文已被整理到了《Android微信開發教程匯總》,歡迎大家學習閱讀。

以上就是本文的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.