Android仿IOS回弹效果 ScrollView回弹 总结

来源:互联网 发布:sql查询身份证号码 编辑:程序博客网 时间:2024/05/17 06:48

Android仿IOS回弹效果  ScrollView回弹 总结

应项目中的需求  需要仿IOS 下拉回弹的效果 , 我在网上搜了很多 大多数都是拿scrollview 改吧改吧

试了一些  发现总有点小问题

下面的代码是我对大家发布的做了点小修改   觉得没太大问题


[java] view plaincopy
  1. package com.example.myscrollview;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Rect;  
  5. import android.util.AttributeSet;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8. import android.view.animation.TranslateAnimation;  
  9. import android.widget.ScrollView;  
  10.   
  11. /** 
  12.  * Bolg :http://blog.csdn.net/aaawqqq?viewmode=contents 
  13.  *  
  14.  * @author baozi 
  15.  *  
  16.  */  
  17.   
  18. public class MyScrollView extends ScrollView {  
  19.   
  20.     // 拖动的距离 size = 4 的意思 只允许拖动屏幕的1/4  
  21.     private static final int size = 4;  
  22.     private View inner;  
  23.     private float y;  
  24.     private Rect normal = new Rect();;  
  25.   
  26.     public MyScrollView(Context context) {  
  27.         super(context);  
  28.     }  
  29.   
  30.     public MyScrollView(Context context, AttributeSet attrs) {  
  31.         super(context, attrs);  
  32.     }  
  33.   
  34.     @Override  
  35.     protected void onFinishInflate() {  
  36.         if (getChildCount() > 0) {  
  37.             inner = getChildAt(0);  
  38.         }  
  39.     }  
  40.   
  41.     @Override  
  42.     public boolean onTouchEvent(MotionEvent ev) {  
  43.         if (inner == null) {  
  44.             return super.onTouchEvent(ev);  
  45.         } else {  
  46.             commOnTouchEvent(ev);  
  47.         }  
  48.         return super.onTouchEvent(ev);  
  49.     }  
  50.   
  51.     public void commOnTouchEvent(MotionEvent ev) {  
  52.         int action = ev.getAction();  
  53.         switch (action) {  
  54.         case MotionEvent.ACTION_DOWN:  
  55.             y = ev.getY();  
  56.             break;  
  57.         case MotionEvent.ACTION_UP:  
  58.             if (isNeedAnimation()) {  
  59.                 // Log.v("mlguitar", "will up and animation");  
  60.                 animation();  
  61.             }  
  62.             break;  
  63.         case MotionEvent.ACTION_MOVE:  
  64.             final float preY = y;  
  65.             float nowY = ev.getY();  
  66.             /** 
  67.              * size=4 表示 拖动的距离为屏幕的高度的1/4 
  68.              */  
  69.             int deltaY = (int) (preY - nowY) / size;  
  70.             // 滚动  
  71.             // scrollBy(0, deltaY);  
  72.   
  73.             y = nowY;  
  74.             // 当滚动到最上或者最下时就不会再滚动,这时移动布局  
  75.             if (isNeedMove()) {  
  76.                 if (normal.isEmpty()) {  
  77.                     // 保存正常的布局位置  
  78.                     normal.set(inner.getLeft(), inner.getTop(),  
  79.                             inner.getRight(), inner.getBottom());  
  80.                     return;  
  81.                 }  
  82.                 int yy = inner.getTop() - deltaY;  
  83.   
  84.                 // 移动布局  
  85.                 inner.layout(inner.getLeft(), yy, inner.getRight(),  
  86.                         inner.getBottom() - deltaY);  
  87.             }  
  88.             break;  
  89.         default:  
  90.             break;  
  91.         }  
  92.     }  
  93.   
  94.     // 开启动画移动  
  95.   
  96.     public void animation() {  
  97.         // 开启移动动画  
  98.         TranslateAnimation ta = new TranslateAnimation(00, inner.getTop(),  
  99.                 normal.top);  
  100.         ta.setDuration(200);  
  101.         inner.startAnimation(ta);  
  102.         // 设置回到正常的布局位置  
  103.         inner.layout(normal.left, normal.top, normal.right, normal.bottom);  
  104.         normal.setEmpty();  
  105.     }  
  106.   
  107.     // 是否需要开启动画  
  108.     public boolean isNeedAnimation() {  
  109.         return !normal.isEmpty();  
  110.     }  
  111.   
  112.     // 是否需要移动布局  
  113.     public boolean isNeedMove() {  
  114.         int offset = inner.getMeasuredHeight() - getHeight();  
  115.         int scrollY = getScrollY();  
  116.         if (scrollY == 0 || scrollY == offset) {  
  117.             return true;  
  118.         }  
  119.         return false;  
  120.     }  
  121.   
  122. }  
  123. // ┏┓   ┏┓  
  124. // ┏┛┻━━━┛┻┓  
  125. // ┃       ┃    
  126. // ┃   ━   ┃  
  127. // ┃ ┳┛ ┗┳ ┃  
  128. // ┃       ┃  
  129. // ┃   ┻   ┃  
  130. // ┃       ┃  
  131. // ┗━┓   ┏━┛  
  132. // ┃   ┃ 神兽保佑          
  133. // ┃   ┃ 代码无BUG!  
  134. // ┃   ┗━━━┓  
  135. // ┃       ┣┓  
  136. // ┃       ┏┛  
  137. // ┗┓┓┏━┳┓┏┛  
  138. // ┃┫┫ ┃┫┫  
  139. // ┗┻┛ ┗┻┛  

代码里面size 这个参数是用来设置拖动的距离    

size= 4 代表  view只会跟随手指滑动1/4的距离

size=3  代表  跟随手指滑动1/3的距离

其它同理

Demo 下载地址:   http://download.csdn.net/detail/aaawqqq/7629533


如果有更好的效果请教我一下 谢谢

祝大家每天都能写出好代码...

0 0
原创粉丝点击