Android自定义ScrollView实现反弹…

本文介绍如何通过自定义Android的ScrollView实现水平滚动时的反弹效果。文中提供了具体的代码实现细节,包括触摸事件处理、动画设置及布局调整等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android的ScrollView控件默认是没有反弹效果的,当滑动到边缘的时候便不能继续滑动。这里通过自定义ScrollView来实现反弹效果。看下面的效果图,红色图片在最左边,android默认ScrollView控件红色图片在最左边的时候是不能向右滚动的。

这里是水平滚动,我们可以通过自定义类继承自HorizontalScrollView类来实现。


  1. public class MyScrollView extends HorizontalScrollView  
  2.     private View inner;  
  3.     private float x;  
  4.     private Rect normal new Rect();  
  5.       
  6.     @Override  
  7.     protected void onFinishInflate()  
  8.         if (getChildCount() 0 
  9.             inner getChildAt(0);  
  10.          
  11.         System.out.println("getChildCount():" getChildCount());  
  12.      
  13.       
  14.     public MyScrollView(Context context, AttributeSet attrs)  
  15.         super(context, attrs);  
  16.      
  17.       
  18.     @Override  
  19.     public boolean onTouchEvent(MotionEvent ev)  
  20.         if (inner == null 
  21.             return super.onTouchEvent(ev);  
  22.         else  
  23.             commOnTouchEvent(ev);  
  24.          
  25.   
  26.         return super.onTouchEvent(ev);  
  27.      
  28.   
  29.     public void commOnTouchEvent(MotionEvent ev)  
  30.         int action ev.getAction();  
  31.         switch (action)  
  32.         case MotionEvent.ACTION_DOWN:  
  33.             ev.getX();  
  34.             break 
  35.         case MotionEvent.ACTION_UP:  
  36.   
  37.             if (isNeedAnimation())  
  38.                 animation();  
  39.              
  40.   
  41.             break 
  42.         case MotionEvent.ACTION_MOVE:  
  43.             final float preX x;  
  44.             float nowX ev.getX();  
  45.             int deltaX (int(preX nowX);  
  46.             // 滚动   
  47.             scrollBy(0deltaX);  
  48.   
  49.             nowX;  
  50.             // 当滚动到最左或者最右时就不会再滚动,这时移动布局   
  51.             if (isNeedMove())  
  52.                 if (normal.isEmpty())  
  53.                     // 保存正常的布局位置   
  54.                     normal.set(inner.getLeft(), inner.getTop(), inner.getRight(), inner.getBottom());  
  55.                  
  56.                 // 移动布局   
  57.                 inner.layout(inner.getLeft() deltaX/2inner.getTop() inner.getRight()- deltaX/2inner.getBottom() );  
  58.              
  59.             break 
  60.   
  61.         default 
  62.             break 
  63.          
  64.      
  65.   
  66.     // 开启动画移动   
  67.     public void animation()  
  68.         // 开启移动动画   
  69.         TranslateAnimation ta new TranslateAnimation(00inner.getTop(), normal.top);  
  70.         ta.setDuration(200);  
  71.         inner.startAnimation(ta);  
  72.         // 设置回到正常的布局位置   
  73.         inner.layout(normal.left, normal.top, normal.right, normal.bottom);  
  74.         normal.setEmpty();  
  75.      
  76.     // 是否需要开启动画   
  77.     public boolean isNeedAnimation()  
  78.         return !normal.isEmpty();  
  79.      
  80.     // 是否需要移动布局   
  81.     public boolean isNeedMove()  
  82.         int offset inner.getMeasuredWidth() getWidth();  
  83.         int scrollX getScrollX();  
  84.         if (scrollX == 0 || scrollX == offset)  
  85.             return true 
  86.          
  87.         return false 
  88.      
  89. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值