編輯:關於 Android 程式設計
近期專案要求支援多圖上傳並以規則的方式呈現,效果要像微信朋友圈那樣整齊排列。這種場景用 GridView 再合適不過——它能動態決定圖片數量,排列 also 相當規律。下面先說明我的整體思路:
下面貼出部分源碼並逐一解析。
<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 與平時使用的略有不同,因此不貼出完整源碼。整體做法同樣是把圖片下載到本地,再以 ImageLoader 載入,只是我這裡額外處理了上傳失敗與新建兩種狀態,所以結構上稍有差異。
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 微信開發教程匯總》,歡迎大家延伸閱讀。以上就是全部內容,希望對大家的學習有所幫助。
Android 微博動態點讚效果:Checkable 與動畫封裝實作
點讚效果的兩個關鍵微博動態的點讚效果看似簡單,但可以拆成兩個重點:一是可切換的選中狀態,二是輕量動畫回饋。本文使用 Checkable 與 AndroidViewAnim
PinnedHeaderListView 源碼解析:仿通訊錄的分組懸浮表頭
GitHub 專案:https://github.com/JimiSmith/PinnedHeaderListView 要做「分組列表、滾動時當前組標題固定懸浮在頂部」的
Android 自定義 View 實現鐘擺風格加載動畫
之前在網上看到一個 iOS 的 PendulumView 組件,小球來回擺動的效果很討喜。原生進度條實在看不上眼,於是琢磨著用自定義 View 復刻一個,之後可以用在頁面
Android AppBarLayout 滑動不順暢問題的排查與優化
在 Android 5.0 之後,AppBarLayout 常被用來製作 Toolbar 與頂部內容的協同滾動效果。搭配 CoordinatorLayout 與可滾動的