網站首頁 編程語言 正文
在ios手機上經常看到頁面上下滑動回彈效果,安卓中沒有原生控件支持,這里自己就去自定義一個scrollview實現回彈效果
1. 新建MyScrollView并繼承ScrollView,可以通過事件分發機制攔截并處理滑動事件
2. 重寫事件分發攔截事件onInterceptTouchEvent方法,計算是否需要攔截事件
//攔截:實現父視圖對子視圖的攔截
//是否攔截成功,取決于方法的返回值。返回值true:攔截成功。反之,攔截失敗
private int lastY;//上一次y軸方向操作的坐標位置
? ? private Rect normal = new Rect();//用于記錄臨界狀態的左、上、右、下
? ? private boolean isFinishAnimation = true;//是否動畫結束
? ? private int lastX, downX, downY;
? ? @Override
? ? public boolean onInterceptTouchEvent(MotionEvent ev) {
? ? ? ? boolean isIntercept = false;
? ? ? ? int eventX = (int) ev.getX();
? ? ? ? int eventY = (int) ev.getY();
? ? ? ? switch (ev.getAction()) {
? ? ? ? ? ? case MotionEvent.ACTION_DOWN:
? ? ? ? ? ? ? ? lastX = downX = eventX;
? ? ? ? ? ? ? ? lastY = downY = eventY;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case MotionEvent.ACTION_MOVE:
? ? ? ? ? ? ? ? //獲取水平和垂直方向的移動距離
? ? ? ? ? ? ? ? int absX = Math.abs(eventX - downX);
? ? ? ? ? ? ? ? int absY = Math.abs(eventY - downY);
? ? ? ? ? ? ? ? if(absY > absX && absY >= dp2px(10)){
? ? ? ? ? ? ? ? ? ? isIntercept = true;//執行攔截
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? lastX = eventX;
? ? ? ? ? ? ? ? lastY = eventY;
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? return isIntercept;
? ? }
3. 得到scrollview的子view,便于操作
?//獲取子視圖
? ? @Override
? ? protected void onFinishInflate() {
? ? ? ? super.onFinishInflate();
? ? ? ? if (getChildCount() > 0) {
? ? ? ? ? ? childView = getChildAt(0);
? ? ? ? }
? ? }
4. 計算是否需要平移動畫
?private boolean isNeedMove() {
? ? ? ? int childMeasuredHeight = childView.getMeasuredHeight();//獲取子視圖的高度
? ? ? ? int scrollViewMeasuredHeight = this.getMeasuredHeight();//獲取布局的高度
? ? ? ? Log.e("TAG", "childMeasuredHeight = " + childMeasuredHeight);
? ? ? ? Log.e("TAG", "scrollViewMeasuredHeight = " + scrollViewMeasuredHeight);
? ? ? ? int dy = childMeasuredHeight - scrollViewMeasuredHeight;//dy >= 0
? ? ? ? int scrollY = this.getScrollY();//獲取用戶在y軸方向上的偏移量 (上 + 下 -)
? ? ? ? if (scrollY <= 0 || scrollY >= dy) {
? ? ? ? ? ? return true;//按照我們自定義的MyScrollView的方式處理
? ? ? ? }
? ? ? ? //其他處在臨界范圍內的,返回false。即表示,仍按照ScrollView的方式處理
? ? ? ? return false;
? ? }
5. 判斷是否需要平移動畫
//判斷是否需要執行平移動畫
? ? private boolean isNeedAnimation() {
? ? ? ? return !normal.isEmpty();
? ? }
6. 既然我們做了事件攔截,那么就要重寫ontouchevent來執行響應事件
@Override
? ? public boolean onTouchEvent(MotionEvent ev) {
? ? ? ? if (childView == null || !isFinishAnimation) {
? ? ? ? ? ? return super.onTouchEvent(ev);
? ? ? ? }
? ? ? ? int eventY = (int) ev.getY();//獲取當前的y軸坐標
? ? ? ? switch (ev.getAction()) {
? ? ? ? ? ? case MotionEvent.ACTION_DOWN:
? ? ? ? ? ? ? ? lastY = eventY;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case MotionEvent.ACTION_MOVE:
? ? ? ? ? ? ? ? int dy = eventY - lastY;//微小的移動量
? ? ? ? ? ? ? ? if (isNeedMove()) {
? ? ? ? ? ? ? ? ? ? if (normal.isEmpty()) {
? ? ? ? ? ? ? ? ? ? ? ? //記錄了childView的臨界狀態的左、上、右、下
? ? ? ? ? ? ? ? ? ? ? ? normal.set(childView.getLeft(), childView.getTop(), childView.getRight(), childView.getBottom());
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? //重新布局
? ? ? ? ? ? ? ? ? ? childView.layout(childView.getLeft(), childView.getTop() + dy / 2, childView.getRight(), childView.getBottom() + dy / 2);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? lastY = eventY;//重新賦值
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case MotionEvent.ACTION_UP:
? ? ? ? ? ? ? ? if (isNeedAnimation()) {
? ? ? ? ? ? ? ? ? ? //使用平移動畫
? ? ? ? ? ? ? ? ? ? int translateY = childView.getBottom() - normal.bottom;
? ? ? ? ? ? ? ? ? ? TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, -translateY);
? ? ? ? ? ? ? ? ? ? translateAnimation.setDuration(200);
// ? ? ? ?translateAnimation.setFillAfter(true);//停留在最終位置上
? ? ? ? ? ? ? ? ? ? translateAnimation.setAnimationListener(new Animation.AnimationListener() {
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public void onAnimationStart(Animation animation) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? isFinishAnimation = false;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public void onAnimationEnd(Animation animation) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? isFinishAnimation = true;
? ? ? ? ? ? ? ? ? ? ? ? ? ? childView.clearAnimation();//清除動畫
? ? ? ? ? ? ? ? ? ? ? ? ? ? //重新布局
? ? ? ? ? ? ? ? ? ? ? ? ? ? childView.layout(normal.left, normal.top, normal.right, normal.bottom);
? ? ? ? ? ? ? ? ? ? ? ? ? ? //清除normal的數據
? ? ? ? ? ? ? ? ? ? ? ? ? ? normal.setEmpty();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public void onAnimationRepeat(Animation animation) {
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? //啟動動畫
? ? ? ? ? ? ? ? ? ? childView.startAnimation(translateAnimation);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? return super.onTouchEvent(ev);
}
這樣整個view的核心部分已經完成了,把view嵌套到定義好了的scrollview就可以實現頁面的滑動回彈效果了。
原文鏈接:https://blog.csdn.net/du1029205592/article/details/83085728
相關推薦
- 2023-02-25 一文搞懂Python中is和==的區別_python
- 2022-04-23 uni-app之條件注釋實現跨端兼容
- 2022-02-03 騰訊云服務器連接失敗,啟動報錯:A start job is running for /etc/rc
- 2022-05-19 pytorch中的?.view()函數的用法介紹_python
- 2022-08-12 Go?內聯優化讓程序員愛不釋手_Golang
- 2022-11-23 Python多線程使用方法詳細講解_python
- 2022-07-19 linux臨時修改網卡
- 2024-01-27 DO、DTO、BO、VO、POJO區別
- 最近更新
-
- 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同步修改后的遠程分支