編輯:關於 Android 程式設計
前陣子實作了一個仿 To 圈個人資料頁的效果,先看看原版的呈現:

這個效果其實花了我一個下午。一開始思路沒理清楚就埋頭硬做,結果事倍功半。這也提醒我:動手前先把邏輯關係想透,再開始寫程式碼,會順利許多。
仔細拆解後,這個效果可以分成三個步驟:
涉及的技術點:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" 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"></LinearLayout> </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"> <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp" android:background="@android:color/white"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="20dp" android:gravity="center_vertical" android:text="守護天使" android:textColor="#DB4E61" android:textSize="14sp"/> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_marginEnd="66dp" android:layout_toStartOf="@+id/imageView" android:gravity="center_vertical" android:text="暫時沒有守護天使" android:textColor="#C0C0C0" android:textSize="14sp"/> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentEnd="true" android:layout_alignParentTop="true" android:layout_marginRight="20dp" android:paddingBottom="34dp" android:paddingTop="34dp" android:src="@drawable/ic_more"/> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#CCCCCC" ></View> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="15dp" android:background="#CCCCCC" ></View> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="@android:color/white" android:orientation="horizontal"> <TextView android:layout_width="80dp" android:layout_height="match_parent" android:layout_marginLeft="15dp" android:gravity="center_vertical" android:text="昵稱" android:textColor="#C0C0C0" android:textSize="14sp"/> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_weight="1" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:textColor="@android:color/black" android:textSize="12dp"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#CCCCCC" ></View> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="@android:color/white" android:orientation="horizontal"> <TextView android:layout_width="80dp" android:layout_height="match_parent" android:layout_marginLeft="15dp" android:gravity="center_vertical" android:text="個性簽名" android:textColor="#C0C0C0" android:textSize="14sp"/> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_weight="1" android:background="@android:color/transparent"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#CCCCCC" ></View> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="15dp" android:background="#CCCCCC" ></View> <LinearLayout android:layout_width="match_parent" android:layout_height="100dp" android:background="@android:color/white" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="35dp" android:layout_marginLeft="15dp" android:gravity="center_vertical" android:text="個人相冊" android:textColor="#C0C0C0" android:textSize="14sp"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#CCCCCC" ></View> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#CCCCCC" ></View> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="15dp" android:background="#CCCCCC" ></View> <LinearLayout android:layout_width="match_parent" android:layout_height="120dp" android:background="@android:color/white" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:layout_marginLeft="15dp" android:gravity="center_vertical" android:text="私房視頻" android:textColor="#DB4E61" android:textSize="14sp"/> <TextView android:layout_width="match_parent" android:layout_height="20dp" android:layout_marginLeft="15dp" android:gravity="start" android:text="別人每查看你的一個私房視頻,你講獲得100朵鮮花" android:textColor="#C0C0C0" android:textSize="10sp"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#CCCCCC" ></View> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#CCCCCC" ></View> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="15dp" android:background="#CCCCCC" ></View> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@android:color/white" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="35dp" android:layout_marginLeft="15dp" android:gravity="center_vertical" android:text="緣分印象" android:textColor="#C0C0C0" android:textSize="14sp"/> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:text="Ta還沒有緣分印象" android:textColor="#C0C0C0"/> </LinearLayout> </LinearLayout> <RelativeLayout android:id="@+id/mainactionbar" android:layout_width="match_parent" android:layout_height="55dp" > <ImageView android:id="@+id/userinfo_topbar" android:visibility="invisible" android:background="@color/colorTheme" android:layout_width="match_parent" android:layout_height="match_parent"/> <ImageButton android:id="@+id/userinfo_returnbtn" android:layout_width="55dp" android:layout_height="55dp" android:padding="15dp" android:scaleType="fitCenter" android:src="@drawable/topbar_returnbtn"/> </RelativeLayout> </RelativeLayout>
這個布局由內而外嵌套了三層,上半部分我直接用截圖呈現:
android:background="@drawable/top"
package com.whale.nangua.toquan; import android.animation.ObjectAnimator; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.ScrollView; import android.widget.Toast; public class MainActivity extends Activity { LinearLayout myscrollLinearlayout; LinearLayout mainheadview; //頂部個人資料視圖 RelativeLayout mainactionbar; //頂部菜單欄 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.userinfo_layout); initView(); } int Y; int position = 0; //拖動Linearlayout的距離Y軸的距離 int scrollviewdistancetotop = 0; //headView的高 int menubarHeight = 0; int chufaHeight = 0; //需要觸發動畫的高 float scale; //像素密度 int headViewPosition = 0; ImageView userinfo_topbar; static boolean flag = true; static boolean topmenuflag = true; private void initView() { userinfo_topbar = (ImageView) findViewById(R.id.userinfo_topbar); //獲得像素密度 scale = this.getResources().getDisplayMetrics().density; mainheadview = (LinearLayout) findViewById(R.id.mainheadview); mainactionbar = (RelativeLayout) findViewById(R.id.mainactionbar); menubarHeight = (int) (55 * scale); chufaHeight = (int) (110 * scale); scrollviewdistancetotop = (int) ((260 )*scale); position = scrollviewdistancetotop; myscrollLinearlayout = (LinearLayout) findViewById(R.id.myscrollLinearlayout); myscrollLinearlayout.setY( scrollviewdistancetotop); //要減去Absolote布局距離頂部的高度 myscrollLinearlayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); myscrollLinearlayout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //按下的Y的位置 Y = (int) event.getRawY(); break; case MotionEvent.ACTION_MOVE: int nowY = (int) myscrollLinearlayout.getY(); //拖動界面的Y軸位置 int tempY = (int) (event.getRawY() - Y); //手移動的偏移量 Y = (int) event.getRawY(); if ((nowY + tempY >= 0) && (nowY + tempY <= scrollviewdistancetotop)) { if ((nowY + tempY <= menubarHeight)&& (topmenuflag == true) ){ userinfo_topbar.setVisibility(View.VISIBLE); topmenuflag = false; } else if ((nowY + tempY > menubarHeight) && (topmenuflag == flag)) { userinfo_topbar.setVisibility(View.INVISIBLE); topmenuflag = true; } int temp = position += tempY; myscrollLinearlayout.setY(temp); int headviewtemp = headViewPosition += (tempY/5); mainheadview.setY(headviewtemp); } //頂部的動畫效果 if ((myscrollLinearlayout.getY() <= chufaHeight) && (flag == true)) { ObjectAnimator anim = ObjectAnimator.ofFloat(mainheadview, "alpha", 1, 0.0f); anim.setDuration(500); anim.start(); flag = false; } else if ((myscrollLinearlayout.getY() > chufaHeight + 40) && (flag == false)) { ObjectAnimator anim = ObjectAnimator.ofFloat(mainheadview, "alpha", 0.0f, 1f); anim.setDuration(500); anim.start(); flag = true; } break; } return false; } }); } }
核心在於對以下三個 Layout 的協同操作:
LinearLayout myscrollLinearlayout; // 底部可拖動的詳細資料
LinearLayout mainheadview; // 頂部個人資料視圖
RelativeLayout mainactionbar; // 頂部菜單欄
先初始化各部分的初始位置,再透過底部拖動布局的觸摸監聽,在其中處理與另外兩個布局的互動:拖動時同步移動頂部頭像區、在越過門檻時切換標題欄的顯示與隱藏,並用 ObjectAnimator 對頭部做淡入淡出的透明度動畫。這部分交互稍嫌繁瑣,程式碼裡都有詳細註解可循。
以下是我實作後的效果圖:

可以看到與 To 圈的原始效果幾乎一模一樣。
以上就是本次分享的 Android 模仿 To 圈個人資料頁層疊淡入淡出效果,希望對大家有所幫助。若有疑問歡迎留言討論。
Android 仿優酷圓形菜單開發學習筆記
先來看看效果: 先分析一下:這個菜單可以分成三層: 1. 一級菜單(最內圈) 2. 二級菜單(中間圈) 3. 三級菜單(最外圈) 一級菜單只有一個按鈕(即 home),
Android 6.0 下載檔案路徑怎麼設?權限機制與完整範例
為什麼 6.0 之後不能「寫死」路徑 在 Android 6.0 之前,開發者只要確認外部儲存空間已經掛載,並在清單檔(AndroidManifest.xml)裡聲明對
不用 WebView 實作 App 內建更新:版本比對、下載與安裝全流程
整體流程只有三步:取得版本號 → 比對版本號 → 下載並安裝新版本。 一、在伺服器放一份版本資訊 要判斷有沒有新版,App 得先知道伺服器上的版本是多少。最簡單的做法是
Android 實現智能機器人聊天完整實作
背景:關於實作聊天機器人,其實是個偶然的機會。公司需要做一個 iOS 版的機器人,用於自動購物,於是 iOS 部門就研發了一個。我覺得這種機器人挺有意思,想搞清楚背後到底