編輯:關於 Android 程式設計
前幾天做了一個仿 To 圈的個人資料界面,效果還算滿意,順手把思路記錄下來。
先看原版的表現:

老實說,這個效果本身難度不高,但我一開始沒理清邏輯就低頭寫代碼,結果走了不少彎路,一個下午才調通。教訓是:先把交互邏輯想透,再動手,效率會高很多。
仔細觀察可以發現,整個效果其實由三件事組成:
1. 下半部分的詳細資料區域可以拖動。
2. 上半部分的頭部背景需要跟隨移動(速度不同,形成視差)。
3. 頂部標題欄由隱藏漸變為顯示。
涉及的技術點則是:屏幕密度 dp 換算、自定義 View 的 OnTouchListener 事件處理,以及透明度屬性動畫。
整個界面由三層嵌套構成,上半部分我直接截了 To 圈的圖片當作頭部背景。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<!-- 上半部分:個人資料頭部 -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="260dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorTheme" />
<LinearLayout
android:id="@+id/mainheadview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/top" />
</RelativeLayout>
<!-- 下半部分:可拖動的詳細資料 -->
<LinearLayout
android:id="@+id/myscrollLinearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEEEEE"
android:clickable="true"
android:orientation="vertical">
<!-- 守護天使、昵稱、個性簽名、個人相冊、私房視頻、緣分印象等分塊 -->
</LinearLayout>
<!-- 頂部菜單欄,默認隱藏 -->
<RelativeLayout
android:id="@+id/mainactionbar"
android:layout_width="match_parent"
android:layout_height="55dp">
<ImageView
android:id="@+id/userinfo_topbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorTheme"
android:visibility="invisible">
<ImageButton
android:id="@+id/userinfo_returnbtn"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="15dp"
android:src="@drawable/topbar_returnbtn" />
</ImageView>
</RelativeLayout>
</RelativeLayout>
頭部背景直接復用切圖:
android:background="@drawable/top"
public class MainActivity extends Activity {
LinearLayout myscrollLinearlayout; // 底部可拖動的詳細資料區
LinearLayout mainheadview; // 頂部個人資料視圖
RelativeLayout mainactionbar; // 頂部菜單欄
ImageView userinfo_topbar;
int Y; // 上次按下的 Y 座標
int position = 0; // 底部布局累計拖動距離
int scrollviewdistancetotop = 0; // 頭部高度,即最大拖動距離
int menubarHeight = 0; // 標題欄高度
int chufaHeight = 0; // 觸發淡出動畫的高度閾值
float scale; // 屏幕像素密度
int headViewPosition = 0; // 頭部當前 Y 偏移
static boolean flag = true; // 頭部是否已淡出
static boolean topmenuflag = true; // 標題欄是否已隱藏
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userinfo_layout);
initView();
}
private void initView() {
userinfo_topbar = findViewById(R.id.userinfo_topbar);
scale = getResources().getDisplayMetrics().density;
mainheadview = findViewById(R.id.mainheadview);
mainactionbar = findViewById(R.id.mainactionbar);
menubarHeight = (int) (55 * scale);
chufaHeight = (int) (110 * scale);
scrollviewdistancetotop = (int) (260 * scale);
position = scrollviewdistancetotop;
myscrollLinearlayout = findViewById(R.id.myscrollLinearlayout);
// 初始位置下移一個頭部高度,讓詳細資料藏到屏幕下方
myscrollLinearlayout.setY(scrollviewdistancetotop);
myscrollLinearlayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Y = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int nowY = (int) myscrollLinearlayout.getY();
int tempY = (int) (event.getRawY() - Y); // 手指移動的偏移量
Y = (int) event.getRawY();
// 限制拖動範圍在 0 到 頭部高度 之間
if (nowY + tempY >= 0 && nowY + tempY <= scrollviewdistancetotop) {
// 拖到足夠高時顯示標題欄
if (nowY + tempY <= menubarHeight && topmenuflag) {
userinfo_topbar.setVisibility(View.VISIBLE);
topmenuflag = false;
} else if (nowY + tempY > menubarHeight && topmenuflag == flag) {
userinfo_topbar.setVisibility(View.INVISIBLE);
topmenuflag = true;
}
position += tempY;
myscrollLinearlayout.setY(position);
// 頭部以 1/5 的速度跟隨,形成視差
headViewPosition += tempY / 5;
mainheadview.setY(headViewPosition);
}
// 頭部淡入淡出
if (myscrollLinearlayout.getY() <= chufaHeight && flag) {
ObjectAnimator anim = ObjectAnimator.ofFloat(mainheadview, "alpha", 1, 0.0f);
anim.setDuration(500);
anim.start();
flag = false;
} else if (myscrollLinearlayout.getY() > chufaHeight + 40 && !flag) {
ObjectAnimator anim = ObjectAnimator.ofFloat(mainheadview, "alpha", 0.0f, 1f);
anim.setDuration(500);
anim.start();
flag = true;
}
break;
}
return false;
}
});
}
}
整個邏輯只圍繞三個布局展開:可拖動的底部詳細資料區、頂部個人資料視圖、頂部菜單欄。
先在初始化階段計算好各部分的尺寸與初始位置(這裡必須用 density 把 dp 換成 px,否則不同分辨率下會錯位),然後把全部交互集中在底部布局的觸摸監聽裡:一邊更新自身 Y 值,一邊按比例驅動頭部視差移動,同時根據當前高度切換標題欄的顯示與否,並在合適的閾值上觸發透明度動畫。
有幾個細節值得留意:回傳 false 而不是 true,是為了不破壞子 View 自身的點擊事件;兩個 static 布爾標誌位用來防止動畫被重複觸發;淡出與淡入的閾值刻意錯開 40dp,避免在臨界點反復跳變。
最終的運行效果:

和原版的表現基本一致。有看不明白的地方歡迎提問。
自製 Android 控件實作:Well Swipe 拖拽排序與刪除動畫
最近動手寫了一個名為 Well Swipe 的客製化控件,主要解決列表項目的手勢互動問題,整體重點放在兩個體驗細節上。 核心功能 這個控件主要實現了兩件事:一是項目左右滑
Android 動畫之 ScaleAnimation 應用詳解
Android 中提供了四種基本動畫:AlphaAnimation(透明度)、ScaleAnimation(縮放)、TranslateAnimation(位移)與 Rot
Android ViewPager 實作自動輪播廣告板(含指示器與手勢互斥)
android-support-v4.jar 是 Google 為相容低版本裝置提供的套件,裡面包含了原本只有 Android 3.0 以上才有的 API,ViewPag
Android 屬性動畫完整指南:ObjectAnimator、ValueAnimator 與 AnimatorSet
Android 的 Property Animation 以改變物件屬性為核心,適合處理顏色、3D 旋轉、位移與透明度等更複雜的動畫需求。 核心概念 Duration: