編輯:關於 Android 開發

如果你仔細觀察過新浪微博的手機客戶端,會發現登入頁有一個很細膩的互動:手指往下拉時,頂部的頭像圖片會跟著被「拉長」,手一放開又平滑地收縮復位。這種手感讓整個畫面充滿生命力。
其實這個效果背後靠的是 GitHub 上一個名為 PullZoomView 的第三方開源專案,專案下載網址為 https://github.com/Frank-Zhu/PullZoomView 。
PullZoomView 主要提供兩種形態:一種是搭配傳統的 Android ListView,另一種則是搭配 ScrollView。本文先聚焦在 ListView 上的用法,也就是 PullToZoomListViewEx。
使用方式與一般 ListView 幾乎相同,只要把自訂的 View 標籤放進你的版面檔即可:
1 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" 2 xmlns:tools="https://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="com.zzw.testpullzoomview.MainActivity" > 6 7 <com.ecloud.pulltozoomview.PullToZoomListViewEx 8 xmlns:custom="https://schemas.android.com/apk/res-auto" 9 android:id="@+id/listView" 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 custom:headerView="@layout/head_view" 13 custom:zoomView="@layout/head_zoom_view" /> 14 15 </RelativeLayout>
這裡有兩個關鍵屬性需要留意。第一個是 custom:headerView,它指向一個放在頭部的子版面:
1 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" 2 android:id="@+id/layout_view" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:layout_gravity="bottom" 6 android:gravity="bottom"> 7 8 <ImageView 9 android:id="@+id/iv_user_head" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:layout_centerInParent="true" 13 android:background="@android:color/holo_red_light" 14 android:src="@drawable/ic_launcher" /> 15 16 <TextView 17 android:id="@+id/tv_user_name" 18 android:textSize="12sp" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:layout_below="@id/iv_user_head" 22 android:layout_centerHorizontal="true" 23 android:text="新浪微博" 24 android:textColor="#ffffff" /> 25 26 <LinearLayout 27 android:id="@+id/ll_action_button" 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content" 30 android:background="#66000000" 31 android:layout_alignParentBottom="true" 32 android:padding="10dip"> 33 34 <TextView 35 android:id="@+id/tv_register" 36 android:layout_width="match_parent" 37 android:layout_height="wrap_content" 38 android:text="注冊" 39 android:layout_weight="1" 40 android:textSize="15sp" 41 android:gravity="center" 42 android:layout_gravity="center" 43 android:textColor="#ffffff" /> 44 45 <TextView 46 android:id="@+id/tv_login" 47 android:layout_width="match_parent" 48 android:layout_height="wrap_content" 49 android:text="登錄" 50 android:layout_weight="1" 51 android:textSize="15sp" 52 android:gravity="center" 53 android:layout_gravity="center" 54 android:textColor="#ffffff" /> 55 </LinearLayout> 56 </RelativeLayout>
要特別理解的是,headerView 本身不會被縮放,它只是一層正常顯示的 Android View。下拉時你會看到它整體往下位移,但尺寸維持不變。
第二個關鍵屬性是 custom:zoomView,這才是真正會產生伸縮變形的那一層:
1 <?xml version="1.0" encoding="utf-8"?> 2 <ImageView xmlns:android="https://schemas.android.com/apk/res/android" 3 android:id="@+id/imageView" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:layout_gravity="center_horizontal" 7 android:scaleType="centerCrop" 8 android:src="@drawable/a" />
head_zoom_view 內容很單純,就是擺一張圖片進去。當使用者下拉時,這張圖會動態地被拉伸與收縮,製造出那種奇妙的視差感。
簡單來說,zoomView 墊在 headerView 的底層:headerView 負責正常顯示內容,zoomView 則負責表演那套伸縮動畫。
1 package com.zzw.testpullzoomview; 2 3 import com.ecloud.pulltozoomview.PullToZoomListViewEx; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.util.DisplayMetrics; 8 import android.util.Log; 9 import android.view.View; 10 import android.widget.AbsListView; 11 import android.widget.AdapterView; 12 import android.widget.AdapterView.OnItemClickListener; 13 import android.widget.ArrayAdapter; 14 15 public class MainActivity extends Activity { 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 PullToZoomListViewEx listView = (PullToZoomListViewEx) findViewById(R.id.listView); 22 23 String data[] = new String[100]; 24 for (int i = 0; i < data.length; i++) { 25 data[i] = "測試數據" + i; 26 } 27 28 listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data)); 29 30 listView.getPullRootView().setOnItemClickListener(new OnItemClickListener() { 31 @Override 32 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 33 Log.d("position", "getPullRootView--->position = " + position); 34 } 35 }); 36 37 listView.setOnItemClickListener(new OnItemClickListener() { 38 @Override 39 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 40 Log.d("position", "position = " + position); 41 } 42 }); 43 44 setPullToZoomListViewExHeaderLayoutParams(listView); 45 } 46 // 設置頭部的View的寬高。 47 private void setPullToZoomListViewExHeaderLayoutParams(PullToZoomListViewEx listView) { 48 DisplayMetrics localDisplayMetrics = new DisplayMetrics(); 49 getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics); 50 int mScreenHeight = localDisplayMetrics.heightPixels; 51 int mScreenWidth = localDisplayMetrics.widthPixels; 52 AbsListView.LayoutParams localObject = new AbsListView.LayoutParams(mScreenWidth, 53 (int) (9.0f * (mScreenWidth / 16.0F))); 54 listView.setHeaderLayoutParams(localObject); 55 } 56 }
這段程式碼示範了如何綁定資料、掛上點擊事件,並透過 setHeaderLayoutParams() 依螢幕寬度以 9:16 的比例設定頭部高度,讓拉伸效果在不同解析度的裝置上都能維持良好比例。
Android 存儲系統解析(一):Vold 與 MountService 的架構與掛載流程
前言 研究 Vold 存儲模組的這段時間,最讓人頭疼的是兩件事:一是整個架構涉及的函式呼叫層層相扣,二是 Kernel 與上層之間透過 Socket 傳輸的資料流需要反覆
挖掘藍牙隱藏 API:用反射實作配對與解除配對
上一篇講了 Android 藍牙的基本用法,這次我們再往下挖一層,看看藍牙相關的隱藏 API。用過 Android 系統設定的人都知道,藍牙搜尋到裝置後可以進行「配對」與
Android 模擬 QQ 介面框架:歡迎頁、底部導覽與 Fragment 切換
整體架構這個範例以類似 QQ 的行動應用為靈感,實作歡迎頁、主頁面與底部導覽切換。畫面主要由 WelcomActivity 與 MainActivity 組成,並搭配多個
Android 動態部署(五):如何從插件 APK 中啟動 Service
Android 動態部署(五):如何從插件 APK 中啟動 Service 經過前幾篇的分析,我們了解到 Google 原生是如何拆分 APK 的,並且可以自己透過解析