Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

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

編輯:關於 Android 程式設計

近期專案要求支援多圖上傳並以規則的方式呈現,效果要像微信朋友圈那樣整齊排列。這種場景用 GridView 再合適不過——它能動態決定圖片數量,排列 also 相當規律。下面先說明我的整體思路:

  • 1. 取得網路圖片
  • 2. 初始化 GridView 並自訂 Adapter
  • 3. 依圖片數量設定 GridView 的欄數
  • 4. 更新 Adapter

下面貼出部分源碼並逐一解析。

一、GridView 的項目佈局

 <com.view.SquareLayout xmlns:android="https://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 是自訂的方形佈局,後面會詳細講解。子項中是一個正方形佈局,裡面嵌套著圖片。

二、自訂 Adapter

由於專案需求各異,這裡的 Adapter 與平時使用的略有不同,因此不貼出完整源碼。整體做法同樣是把圖片下載到本地,再以 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 的每一個項目。

接著看效果圖:

ImageView 的 scaleType 若設為 fitXY 會填滿方形佈局,設為 center 則居中顯示。下面逐一說明各屬性差異:

1)center:保持原圖大小,顯示於 ImageView 中央;若原圖尺寸大於 ImageView,超出部分會被裁切。

2)centerCrop:以填滿整個 ImageView 為目標,將原圖中心對齊 ImageView 中心,等比例放大直到填滿為止,超出部分裁切處理。

3)centerInside:以完整顯示圖片為目標,將內容居中顯示,透過縮放讓圖片寬(高)等於或小於 ImageView;若原圖本身較小,則不縮放,直接居中顯示。

4)matrix:不改變原圖大小,從 ImageView 左上角開始繪製,超出部分裁切處理。

5)fitCenter:將原圖按比例縮放至 ImageView 尺寸,並居中顯示。

6)fitEnd:將原圖按比例縮放至 ImageView 尺寸,顯示於下方位置。

7)fitStart:將原圖按比例縮放至 ImageView 尺寸,顯示於上方位置。

8)fitXY:不保持原比例,直接拉伸填滿 ImageView。

本文已整理進《Android 微信開發教程匯總》,歡迎大家延伸閱讀。以上就是全部內容,希望對大家的學習有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved