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

實現美團風格的「立即搶購」懸浮吸頂效果

編輯:關於 Android 程式設計

行動網路普及之後,走在路上人人都在滑手機,「低頭族」這個詞大概就是這麼來的。身為一名開發者,通勤時我也愛看那些高人氣 App 的介面——人家做得好,用戶體驗與 UI 設計功不可沒。前陣子在美團和到大眾點評看到一個很順手的效果,於是就自己動手做了一遍。

看圖(2):當「立即搶購」那塊往上滑到導覽列下方時,它就黏在導覽列底邊不動了,下面的內容繼續滾動;反向滑動時,它又跟著離開。乍看有點複雜,但思路說穿了很簡單:

滑動過程中持續判斷「立即搶購」是否頂到導覽列——一旦頂到,就在導覽列下方疊一個一模一樣的懸浮版;往下滑動,等原本那塊的下緣回到懸浮版位置時,再把懸浮版移掉。這樣視覺上就變成吸頂。

新建專案 MeiTuanDemo。先看立即搶購的 buy_layout.xml(圖片直接截自美團):

 <?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/buy_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/buy" /> </LinearLayout> 

主畫面版面:上方導覽列,其餘內容都放進自定義的 MyScrollView。

 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:scaleType="centerCrop" android:layout_width="match_parent" android:layout_height="45dip" android:src="@drawable/navigation_bar" /> <com.example.meituandemo.MyScrollView android:id="@+id/scrollView" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/iamge" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/pic" android:scaleType="centerCrop" /> <include android:id="@+id/buy" layout="@layout/buy_layout" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/one" android:scaleType="centerCrop" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/one" android:scaleType="centerCrop" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/one" android:scaleType="centerCrop" /> </LinearLayout> </com.example.meituandemo.MyScrollView> </LinearLayout> 

注意到容器不是 ScrollView 而是 MyScrollView。ScrollView 並沒有提供滾動監聽,所以必須自己補一個:

 package com.example.meituandemo; import android.content.Context; import android.os.Handler; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.ScrollView; /** * 博客地址:https://blog.csdn.net/xiaanming * * @author xiaanming * */ public class MyScrollView extends ScrollView { private OnScrollListener onScrollListener; /** * 主要是用在用戶手指離開MyScrollView,MyScrollView還在繼續滑動,我們用來保存Y的距離,然後做比較 */ private int lastScrollY; public MyScrollView(Context context) { this(context, null); } public MyScrollView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } /** * 設置滾動接口 * @param onScrollListener */ public void setOnScrollListener(OnScrollListener onScrollListener) { this.onScrollListener = onScrollListener; } /** * 用於用戶手指離開MyScrollView的時候獲取MyScrollView滾動的Y距離,然後回調給onScroll方法中 */ private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { int scrollY = MyScrollView.this.getScrollY(); //此時的距離和記錄下的距離不相等,在隔5毫秒給handler發送消息 if(lastScrollY != scrollY){ lastScrollY = scrollY; handler.sendMessageDelayed(handler.obtainMessage(), 5); } if(onScrollListener != null){ onScrollListener.onScroll(scrollY); } }; }; /** * 重寫onTouchEvent, 當用戶的手在MyScrollView上面的時候, * 直接將MyScrollView滑動的Y方向距離回調給onScroll方法中,當用戶抬起手的時候, * MyScrollView可能還在滑動,所以當用戶抬起手我們隔5毫秒給handler發送消息,在handler處理 * MyScrollView滑動的距離 */ @Override public boolean onTouchEvent(MotionEvent ev) { if(onScrollListener != null){ onScrollListener.onScroll(lastScrollY = this.getScrollY()); } switch(ev.getAction()){ case MotionEvent.ACTION_UP: handler.sendMessageDelayed(handler.obtainMessage(), 5); break; } return super.onTouchEvent(ev); } /** * * 滾動的回調接口 * * @author xiaanming * */ public interface OnScrollListener{ /** * 回調方法, 返回MyScrollView滑動的Y方向距離 * @param scrollY * 、 */ public void onScroll(int scrollY); } } 

這段有個容易被忽略的細節:手指離開後 ScrollView 還會因慣性繼續滑一段,所以只在 onTouchEvent 裡監聽並不足夠。這裡的做法是 ACTION_UP 時啟動 Handler,每 5 毫秒比對一次 scrollY,直到不再變動為止。

有了滾動距離,剩下的就是決定何時加、何時移除懸浮框:

 package com.example.meituandemo; import android.app.Activity; import android.content.Context; import android.graphics.PixelFormat; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.view.WindowManager.LayoutParams; import android.widget.LinearLayout; import com.example.meituandemo.MyScrollView.OnScrollListener; /** * 博客地址:https://blog.csdn.net/xiaanming * * @author xiaanming * */ public class MainActivity extends Activity implements OnScrollListener{ private MyScrollView myScrollView; private LinearLayout mBuyLayout; private WindowManager mWindowManager; /** * 手機屏幕寬度 */ private int screenWidth; /** * 懸浮框View */ private static View suspendView; /** * 懸浮框的參數 */ private static WindowManager.LayoutParams suspendLayoutParams; /** * 購買布局的高度 */ private int buyLayoutHeight; /** * myScrollView與其父類布局的頂部距離 */ private int myScrollViewTop; /** * 購買布局與其父類布局的頂部距離 */ private int buyLayoutTop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myScrollView = (MyScrollView) findViewById(R.id.scrollView); mBuyLayout = (LinearLayout) findViewById(R.id.buy); myScrollView.setOnScrollListener(this); mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); screenWidth = mWindowManager.getDefaultDisplay().getWidth(); } /** * 窗口有焦點的時候,即所有的布局繪制完畢的時候,我們來獲取購買布局的高度和myScrollView距離父類布局的頂部位置 */ @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if(hasFocus){ buyLayoutHeight = mBuyLayout.getHeight(); buyLayoutTop = mBuyLayout.getTop(); myScrollViewTop = myScrollView.getTop(); } } /** * 滾動的回調方法,當滾動的Y距離大於或者等於 購買布局距離父類布局頂部的位置,就顯示購買的懸浮框 * 當滾動的Y的距離小於 購買布局距離父類布局頂部的位置加上購買布局的高度就移除購買的懸浮框 * */ @Override public void onScroll(int scrollY) { if(scrollY >= buyLayoutTop){ if(suspendView == null){ showSuspend(); } }else if(scrollY <= buyLayoutTop + buyLayoutHeight){ if(suspendView != null){ removeSuspend(); } } } /** * 顯示購買的懸浮框 */ private void showSuspend(){ if(suspendView == null){ suspendView = LayoutInflater.from(this).inflate(R.layout.buy_layout, null); if(suspendLayoutParams == null){ suspendLayoutParams = new LayoutParams(); suspendLayoutParams.type = LayoutParams.TYPE_PHONE; //懸浮窗的類型,一般設為2002,表示在所有應用程序之上,但在狀態欄之下 suspendLayoutParams.format = PixelFormat.RGBA_8888; suspendLayoutParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE; //懸浮窗的行為,比如說不可聚焦,非模態對話框等等 suspendLayoutParams.gravity = Gravity.TOP; //懸浮窗的對齊方式 suspendLayoutParams.width = screenWidth; suspendLayoutParams.height = buyLayoutHeight; suspendLayoutParams.x = 0; //懸浮窗X的位置 suspendLayoutParams.y = myScrollViewTop; ////懸浮窗Y的位置 } } mWindowManager.addView(suspendView, suspendLayoutParams); } /** * 移除購買的懸浮框 */ private void removeSuspend(){ if(suspendView != null){ mWindowManager.removeView(suspendView); suspendView = null; } } } 

幾個關鍵時機:在 onWindowFocusChanged 才量 buyLayoutTop 與 buyLayoutHeight,因為版面必須先跑完一次繪製才拿得到正確數值;懸浮框靠 WindowManager.addView / removeView 加入與移除,y 座標對齊 myScrollViewTop。

執行前別忘了在 AndroidManifest.xml 加上:

 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

跑起來的效果:

補充兩點實務建議:WindowManager 加蓋的 View 屬於系統層級,Android 6.0 之後 SYSTEM_ALERT_WINDOW 需要另外引導使用者到設定頁手動授予「顯示在其他應用程式上層」;若這個懸浮效果只在自家 App 內使用,其實改用 CoordinatorLayout 搭配 AppBarLayout 的 collapsing 寫法會更省事,也不必冒權限風險。

專案源碼,點擊下載。

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