注解   

/**
     * Scroll the view with standard behavior for scrolling beyond the normal
     * content boundaries. Views that call this method should override
     * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
     * results of an over-scroll operation.
     *
     * Views can use this method to handle any touch or fling-based scrolling.
     *
     * @param deltaX Change in X in pixels
     * @param deltaY Change in Y in pixels
     * @param scrollX Current X scroll value in pixels before applying deltaX
     * @param scrollY Current Y scroll value in pixels before applying deltaY
     * @param scrollRangeX Maximum content scroll range along the X axis
     * @param scrollRangeY Maximum content scroll range along the Y axis
     * @param maxOverScrollX Number of pixels to overscroll by in either direction
     *          along the X axis.
     * @param maxOverScrollY Number of pixels to overscroll by in either direction
     *          along the Y axis.
     * @param isTouchEvent true if this scroll operation is the result of a touch event.
     * @return true if scrolling was clamped to an over-scroll boundary along either
     *          axis, false otherwise.
     */
@SuppressWarnings({"UnusedParameters"})
    protected boolean overScrollBy(int deltaX, int deltaY,
            int scrollX, int scrollY,
            int scrollRangeX, int scrollRangeY,
            int maxOverScrollX, int maxOverScrollY,
            boolean isTouchEvent) 


maxOverScrollX、maxOverScrollY默认为0




注:

只有当listview不能单一屏幕显示时才有该效果




转载内容

第一部分  原理分析 

      IOS上的bounce功能给人的感觉很爽,当一个可以滚动的区域被拖到边界时,它允许用户将内容拖过界,放手后再弹回来,以一种非常棒的方式提示了用户边界的存在,是IOS的一大特色。android2.3新增了overscroll功能,听名字就知道应该是bounce功能的翻版,但也许是出于专利方面的考虑,google的默认实现跟IOS有所不同,它只是在list拖到边界处时做了一个发光的动画,个人觉得体验比IOS差远了。而且这个黄色的发光在黑色背景下虽然效果不错,在其它背景下可就难说了,因此很多人想要关掉它。 

      日前google上搜索“android overscroll”,对此效果的介绍很多,但关于其具体使用方式和实现,则很少涉及,偶有提及,也经常答非所问或似是而非,反而误导了别人。于是我查阅了android相关源码,并做了一些测试,在此讲讲我的理解。 

      首先是overscroll功能本身,在最顶层的View类提供了支持,可通过setOverScrollMode函数控制其出现条件。但其实View中并没有实现overscroll功能,它仅仅提供了一个辅助函数overScrollBy,该函数根据overScrollMode和内容是否需要滚动控制最大滚动范围,最后将计算结果传给onOverScrolled实现具体的overscroll功能,但此函数在View类中是全空的。 

      overscroll功能真正的实现分别在ScrollView、AbsListView、HorizontalScrollView和WebView中各有一份,代码基本一样。以ScrollView为例,它在处理笔点移动消息时调用overScrollBy来滚动视图,然后重载了overScrollBy函数来实现具体功能,其位置计算通过OverScroller类实现。OverScroller作为一个计算引擎,应该是一个独立的模块,具体滚动效果和范围都不可能通过它来设置,我觉得没有必要细看。但滚动位置最终是它给出的,那相关数据肯定要传递给它,回头看overScrollBy函数,它有两个控制overScroll出界范围的参数,几个实现里面都是取自ViewConfiguration.getScaledOverscrollDistance,而这个参数的值在我的源码中都是0,而且我没找到任何可以影响其结果的设置。 

      真悲催,绕了半天,android的默认实现里面根本没有给出overscroll功能,它只是提供了实现机制,要想用起来还得应用程序自己显式重写相关控件,估计还有一层隐含的意思,法律风险自负。在我的系统中一试,果然一个像素都不能拉出界。但那个闪光是怎么回事呢? 

      在处理笔点消息处,overScrollBy后面不远处有一段mEdgeGlowTop的操作代码,看名字就像,用它一搜,相关机制就全明白了。mEdgeGlowTop在setOverScrollMode函数时创建,它使用的图片都是系统中固有的,甚至不能通过theme改变。它的实现原理也很简单,仅仅是两张png图片的合成,通过透明度的变化制造闪光的效果。更无语的是它既不能被应用程序访问,也不受任何控制,要关闭它的唯一办法是setOverScrollMode(View.OVER_SCROLL_NEVER)。否则就重写onTouchEvent函数吧,想干啥都可以,只是得自己做。 

      谈到overScroll,很多文章都提到了ListView的setOverscrollHeader和setOverscrollFooter,很多人想通过这个来控制那个闪光效果。这两玩意不但可以通过函数设置,也可以在xml中指定,相当方便。但最后很多人发现没有任何作用,百思不得其解。其实这两张图片是用来作为overScroll拖过界时的背景的,默认系统不能拖过界,自然永远都看不到,有些定制的系统中能拖出界几个像素,但也很难看清。 

                 第二部分  代码实现  
     1. 在View中增加了overSrollBy方法,用于记录x, y 轴上滚动。 

     2. 在AbsListView的onTouchEvent中判断是否到达边界(顶部 或 底部) ,然后调用view.overScrollBy ,传入 mScrollY等参数 

     3. overScrollBy 最终赋值给View的mScrollX, mScrollY 两个变量 

     4. 在AbsListView中调用完overScrollBy之后,调用invalidate重绘 



自定义ListView  
Java代码  
  1. public class BounceListView extends ListView{  
  2.     private static final int MAX_Y_OVERSCROLL_DISTANCE = 200;  
  3.        
  4.     private Context mContext;  
  5.     private int mMaxYOverscrollDistance;  
  6.        
  7.     public BounceListView(Context context){  
  8.         super(context);  
  9.         mContext = context;  
  10.         initBounceListView();  
  11.     }  
  12.        
  13.     public BounceListView(Context context, AttributeSet attrs){  
  14.         super(context, attrs);  
  15.         mContext = context;  
  16.         initBounceListView();  
  17.     }  
  18.        
  19.     public BounceListView(Context context, AttributeSet attrs, int defStyle){  
  20.         super(context, attrs, defStyle);  
  21.         mContext = context;  
  22.         initBounceListView();  
  23.     }  
  24.        
  25.     private void initBounceListView(){  
  26.         //get the density of the screen and do some maths with it on the max overscroll distance  
  27.         //variable so that you get similar behaviors no matter what the screen size  
  28.            
  29.         final DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();  
  30.         final float density = metrics.density;  
  31.            
  32.         mMaxYOverscrollDistance = (int) (density * MAX_Y_OVERSCROLL_DISTANCE);  
  33.     }  
  34.        
  35.     @Override  
  36.     protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent){   
  37.         //This is where the magic happens, we have replaced the incoming maxOverScrollY with our own custom variable mMaxYOverscrollDistance;   
  38.         return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, mMaxYOverscrollDistance, isTouchEvent);    
  39.     }  
  40.        
  41. }  


Java代码  
  1. public class MainActivity extends Activity {    
  2.     
  3.     @Override    
  4.     public void onCreate(Bundle savedInstanceState) {    
  5.         super.onCreate(savedInstanceState);    
  6.             
  7.         LinearLayout linearLayout = new LinearLayout(this);    
  8.         linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));    
  9.             
  10.         setContentView(linearLayout);    
  11.             
  12.             
  13.         BounceListView bounceListView = new BounceListView(this);    
  14.             
  15.         String[] data = new String[30];    
  16.         for (int i = 0; i < data.length; i++) {    
  17.             data[i] = "回弹效果 " + i;    
  18.         }    
  19.             
  20.         ArrayAdapter arrayAdapter =     
  21.                 new ArrayAdapter(this,android.R.layout.simple_list_item_1, data);    
  22.             
  23.         bounceListView.setAdapter(arrayAdapter);    
  24.             
  25.         linearLayout.addView(bounceListView);    
  26.     }    
  27.         
  28.         
  29.         
  30. }    

更多相关文章

  1. Android布局文件.xml中的自定义属性(结合封装的自定义View)
  2. Android(安卓)监听锁屏、解锁、开屏 功能代码
  3. android获取recycleview滚动的距离
  4. Qt在Android平台上实现html转PDF的功能
  5. ---RIL层代码分析---RIL_startEventLoop()->eventLoop()->ril_ev
  6. smartrefreshlayout 只开启纯滚动模式
  7. 2020.05.11-Python中调用zip包中的Python脚本
  8. ViewFlipper实现View轮播点击等效果
  9. JAVA按钮显示用户名+密码

随机推荐

  1. php读取大文件的行数的方法
  2. PHP获取字符串中字符、字符串第n次出现的
  3. 用php输出一个数组中的偶数或奇数的方法
  4. 如何隐藏php版本信息
  5. PHP-curl通过GET或POST方式实现http及htt
  6. php中强制字母转换大小写的方法有哪些
  7. php如何处理wsdl
  8. 用PHP做负载均衡指南
  9. php中如何判断字母是大写还是小写
  10. php中按大小进行排序的函数有哪些