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

Android 實現仿 iOS 反彈效果的 ScrollView 教程

編輯:關於 Android 程式設計

先來看看本文最終要實現的效果圖:

這就是 iOS 中經典的回彈效果——滾動到頂部或底部時,內容會有一個自然的回彈過渡,而不像原生 ScrollView 那樣生硬地戛然而止。由於系統自帶的 ScrollView 無法實現這種效果,我們需要自定義一個 View 並繼承 ScrollView。

一、自定義 BounceScrollView

核心思路是:監控滾動到頂部或底部時的觸摸移動,手動平移內部子 View 製造「拉開」的視覺,手指鬆開後再通過平移動畫回到原位,從而模擬 iOS 的回彈手感。

 package davidbouncescrollview.qq986945193.com.davidbouncescrollview; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Rect; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.animation.TranslateAnimation; import android.widget.ScrollView; /** * @author :程序員小冰 * @新浪微博 :https://weibo.com/mcxiaobing * @GitHub:https://github.com/QQ986945193 * @CSDN博客: https://blog.csdn.net/qq_21376985 * @交流Qq :986945193 * 類名:帶有反彈效果的scrollview */ public class BounceScrollView extends ScrollView { private View inner;// 孩子View private float y;// 點擊時y坐標 private Rect normal = new Rect();// 矩形(這裡只是個形式,只是用於判斷是否需要動畫.) private boolean isCount = false;// 是否開始計算 public BounceScrollView(Context context, AttributeSet attrs) { super(context, attrs); } /*** * 根據 XML 生成視圖工作完成.該函數在生成視圖的最後調用,在所有子視圖添加完之後. 即使子類覆蓋了 onFinishInflate * 方法,也應該調用父類的方法,使該方法得以執行. */ @SuppressLint("MissingSuperCall") @Override protected void onFinishInflate() { if (getChildCount() > 0) { inner = getChildAt(0); } } /*** * 監聽touch */ @Override public boolean onTouchEvent(MotionEvent ev) { if (inner != null) { commOnTouchEvent(ev); } return super.onTouchEvent(ev); } /*** * 觸摸事件 * * @param ev */ public void commOnTouchEvent(MotionEvent ev) { int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_UP: // 手指松開. if (isNeedAnimation()) { animation(); isCount = false; } break; /*** * 排除出第一次移動計算,因為第一次無法得知y坐標, 在MotionEvent.ACTION_DOWN中獲取不到, * 因為此時是MyScrollView的touch事件傳遞到到了LIstView的孩子item上面.所以從第二次計算開始. * 然而我們也要進行初始化,就是第一次移動的時候讓滑動距離歸0. 之後記錄准確了就正常執行. * https://github.com/QQ986945193 */ case MotionEvent.ACTION_MOVE: final float preY = y;// 按下時的y坐標 float nowY = ev.getY();// 時時y坐標 int deltaY = (int) (preY - nowY);// 滑動距離 if (!isCount) { deltaY = 0; // 在這裡要歸0. } y = nowY; // 當滾動到最上或者最下時就不會再滾動,這時移動布局 if (isNeedMove()) { // 初始化頭部矩形 if (normal.isEmpty()) { // 保存正常的布局位置 normal.set(inner.getLeft(), inner.getTop(), inner.getRight(), inner.getBottom()); } // Log.e("jj", "矩形:" + inner.getLeft() + "," + inner.getTop() // + "," + inner.getRight() + "," + inner.getBottom()); // 移動布局 inner.layout(inner.getLeft(), inner.getTop() - deltaY / 2, inner.getRight(), inner.getBottom() - deltaY / 2); } isCount = true; break; default: break; } } /*** * 回縮動畫 */ public void animation() { // 開啟移動動畫 TranslateAnimation ta = new TranslateAnimation(0, 0, inner.getTop(), normal.top); ta.setDuration(200); inner.startAnimation(ta); // 設置回到正常的布局位置 inner.layout(normal.left, normal.top, normal.right, normal.bottom); // Log.e("jj", "回歸:" + normal.left + "," + normal.top + "," + normal.right // + "," + normal.bottom); normal.setEmpty(); } // 是否需要開啟動畫 public boolean isNeedAnimation() { return !normal.isEmpty(); } /*** * 是否需要移動布局 inner.getMeasuredHeight():獲取的是控件的總高度 * <p/> * getHeight():獲取的是屏幕的高度 * <p/> * https://github.com/QQ986945193 * * @return */ public boolean isNeedMove() { int offset = inner.getMeasuredHeight() - getHeight(); int scrollY = getScrollY(); // Log.e("jj", "scrolly=" + scrollY); // 0是頂部,後面那個是底部 if (scrollY == 0 || scrollY == offset) { return true; } return false; } }

二、使用方法

BounceScrollView 的用法和普通的 ScrollView 完全一樣,直接在布局文件中引用即可:

 <?xml version="1.0" encoding="utf-8"?> <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="davidbouncescrollview.qq986945193.com.davidbouncescrollview.MainActivity"> <davidbouncescrollview.qq986945193.com.davidbouncescrollview.BounceScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/black" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="大家好,我是程序員小冰" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/black" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="大家好,我是程序員小冰" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/black" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="大家好,我是程序員小冰" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/black" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="大家好,我是程序員小冰" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/black" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="大家好,我是程序員小冰" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/black" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="大家好,我是程序員小冰" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/black" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="大家好,我是程序員小冰" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/black" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="大家好,我是程序員小冰" /> </LinearLayout> </davidbouncescrollview.qq986945193.com.davidbouncescrollview.BounceScrollView> </LinearLayout>

寫好之後直接運行,就能看到頂部與底部自然的回彈效果了。

(AndroidStudio 版)GitHub 下載地址:https://github.com/QQ986945193/DavidBounceScrollView

以上就是關於實現仿 iOS 回彈效果 ScrollView 的完整教程,希望對大家有所幫助,如有疑問歡迎留言交流。

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