Android自定义ScrollView实现反弹效果【附源码】


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

Android自定义ScrollView实现反弹效果

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

源码下载地址:

**************************************************************

下载在帮客之家的1号FTP服务器里,下载地址:

FTP地址:ftp://www.bkjia.com

用户名:www.bkjia.com

密码:www.muu.cc

在 2012年LinuxIDC.com\3月\Android自定义ScrollView实现反弹效果【附源码】

下载方法见 http://www.bkjia.net/thread-1187-1-1.html

**************************************************************

  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.             x = 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(0, deltaX);  
  48.   
  49.             x = 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/2, inner.getTop() , inner.getRight()- deltaX/2, inner.getBottom() );  
  58.             }  
  59.             break;  
  60.   
  61.         default:  
  62.             break;  
  63.         }  
  64.     }  
  65.   
  66.     // 开启动画移动   
  67.     public void animation() {  
  68.         // 开启移动动画   
  69.         TranslateAnimation ta = new TranslateAnimation(00, inner.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. }  

相关内容