網站首頁 編程語言 正文
Android開發中,當一個頁面存放的控件超出屏幕時,通常需要使用ScrollView來包裹布局。這樣用戶可以通過手指的滑動來查看超出屏幕的部分。然而當ScrollView滑動到邊界時,繼續滑動只會顯示一個陰影效果。iOS自帶的控件卻可以實現邊界的阻尼回彈效果,這種阻尼回彈效果會讓用戶有更好的使用體驗。這里給出一個Android上的實現方案
解決思路:
ScrollView使用時要求內部有且僅一個子View。當ScrollView滑動到邊界時,讓子View在ScrollView中隨著手指按一定的規則進行平移,模擬出拉伸效果。當手指松開時,再讓子View恢復拉伸前的位置,模擬出回彈效果。
完整的代碼如下,詳細的原理見注釋即可
public class StretchScrollView extends NestedScrollView { ? ? // 子View ? ? private View innerView; ? ? // 上次手勢事件的y坐標 ? ? private float mLastY; ? ? // 記錄子View的正常位置 ? ? private Rect normal = new Rect(); ? ? public StretchScrollView(Context context, AttributeSet attrs) { ? ? ? ? super(context, attrs); ? ? } ? ? @Override ? ? protected void onFinishInflate() { ? ? ? ? initView(); ? ? ? ? super.onFinishInflate(); ? ? } ? ? /** ? ? ?* 獲取ScrollView的子布局 ? ? ?*/ ? ? private void initView() { ? ? ? ? // 去除原本ScrollView滾動到邊界時的陰影效果 ? ? ? ? setOverScrollMode(OVER_SCROLL_NEVER); ? ? ? ? if (getChildAt(0) != null) { ? ? ? ? ? ? innerView = getChildAt(0); ? ? ? ? } ? ? } ? ? @Override ? ? public boolean onTouchEvent(MotionEvent ev) { ? ? ? ? switch (ev.getAction()) { ? ? ? ? ? ? case MotionEvent.ACTION_UP: ? ? ? ? ? ? ? ? // 手指松開恢復 ? ? ? ? ? ? ? ? if (!normal.isEmpty()) { ? ? ? ? ? ? ? ? ? ? planAnimation(); ? ? ? ? ? ? ? ? ? ? normal.setEmpty(); ? ? ? ? ? ? ? ? ? ? mLastY = 0; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? ? ? float currentY = ev.getY(); ? ? ? ? ? ? ? ? // 滑動距離 ? ? ? ? ? ? ? ? int distanceY = (int) (mLastY - currentY); ? ? ? ? ? ? ? ? // 處理Y軸的滾動事件,當滾動到最上或者最下時需要移動布局 ? ? ? ? ? ? ? ? // 手指剛觸及屏幕時,也會觸發此事件,此時mLastY的值還是0,會立即觸發一個比較大的移動。這里過濾掉這種情況 ? ? ? ? ? ? ? ? if (isNeedTranslate() && mLastY != 0) { ? ? ? ? ? ? ? ? ? ? if (normal.isEmpty()) { ? ? ? ? ? ? ? ? ? ? ? ? // 保存正常的布局位置 ? ? ? ? ? ? ? ? ? ? ? ? normal.set(innerView.getLeft(), innerView.getTop(), innerView.getRight(), innerView.getBottom()); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? // 移動布局, 使distance / 2 防止平移過快 ? ? ? ? ? ? ? ? ? ? innerView.layout(innerView.getLeft(), innerView.getTop() - distanceY / 2, ? ? ? ? ? ? ? ? ? ? ? ? ? ? innerView.getRight(), innerView.getBottom() - distanceY / 2); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? mLastY = currentY; ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? return super.onTouchEvent(ev); ? ? } ? ? /** ? ? ?* 回縮動畫 ? ? ?*/ ? ? public void planAnimation() { ? ? ? ? // 開啟移動動畫 ? ? ? ? TranslateAnimation animation = new TranslateAnimation(0, 0, innerView.getTop(), normal.top); ? ? ? ? animation.setDuration(200); ? ? ? ? innerView.startAnimation(animation); ? ? ? ? // 補間動畫并不會真正修改innerView的位置,這里需要設置使得innerView回到正常的布局位置 ? ? ? ? innerView.layout(normal.left, normal.top, normal.right, normal.bottom); ? ? } ? ? /** ? ? ?* 是否需要Y移動布局 ? ? ?*/ ? ? public boolean isNeedTranslate() { ? ? ? ? int offset = innerView.getMeasuredHeight() - getHeight(); ? ? ? ? int scrollY = getScrollY(); ? ? ? ? // 頂部或者底部 ? ? ? ? return scrollY == 0 || scrollY == offset; ? ? } }
原文鏈接:https://blog.csdn.net/YX_BB/article/details/104698970
相關推薦
- 2022-03-15 BeanCreationException或NoSuchBeanDefinitionExceptio
- 2022-02-13 C語言-操作符(詳細)和表達式求值
- 2022-07-09 docker安裝nginx并部署前端項目的全過程_docker
- 2022-11-18 Shell實現批量操作文件的方法詳解_linux shell
- 2022-07-19 Linux——磁盤管理與文件系統
- 2023-10-09 雙token登錄
- 2022-11-23 Python字典高級用法深入分析講解_python
- 2022-08-19 React組件通信
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支