Android仿IOS上拉下拉弹性效果


本文转自:http://blog.csdn.net/u014733374/article/details/42739345


用过iphone的朋友相信都体验过页面上拉下拉有一个弹性的效果,使用起来用户体验很好;Android并没有给我们封装这样一个效果,我们来看下在Android里如何实现这个效果。先看效果,感觉有些时候还是蛮实用的。

    思路:其实原理很简单,实现一个自定义的Scrollview方法(来自网上大神),然后在布局文件中使用自定义方法Scrollview就可以了。
    代码:
    自定义View,继承自Scrollview。MyReboundScrollView类
[java]  view plain  copy
  1. package com.wj.myreboundscrollview.customview;  
  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. //仿ios可上提下拉的ScrollView  
  12. public class MyReboundScrollView extends ScrollView {  
  13.       
  14.     private static final String TAG = "ElasticScrollView";  
  15.       
  16.     //移动因子, 是一个百分比, 比如手指移动了100px, 那么View就只移动50px  
  17.     //目的是达到一个延迟的效果  
  18.     private static final float MOVE_FACTOR = 0.5f;  
  19.        
  20.     //松开手指后, 界面回到正常位置需要的动画时间  
  21.     private static final int ANIM_TIME = 100;  
  22.        
  23.     //ScrollView的子View, 也是ScrollView的唯一一个子View  
  24.     private View contentView;   
  25.        
  26.     //手指按下时的Y值, 用于在移动时计算移动距离  
  27.     //如果按下时不能上拉和下拉, 会在手指移动时更新为当前手指的Y值  
  28.     private float startY;  
  29.        
  30.     //用于记录正常的布局位置  
  31.     private Rect originalRect = new Rect();  
  32.        
  33.     //手指按下时记录是否可以继续下拉  
  34.     private boolean canPullDown = false;  
  35.        
  36.     //手指按下时记录是否可以继续上拉  
  37.     private boolean canPullUp = false;  
  38.        
  39.     //在手指滑动的过程中记录是否移动了布局  
  40.     private boolean isMoved = false;  
  41.    
  42.     public MyReboundScrollView(Context context) {  
  43.         super(context);  
  44.     }  
  45.        
  46.     public MyReboundScrollView(Context context, AttributeSet attrs) {  
  47.         super(context, attrs);  
  48.     }  
  49.    
  50.     @Override  
  51.     protected void onFinishInflate() {  
  52.         if (getChildCount() > 0) {  
  53.             contentView = getChildAt(0);  
  54.         }  
  55.     }  
  56.        
  57.     @Override  
  58.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  59.         super.onLayout(changed, l, t, r, b);  
  60.            
  61.         if(contentView == nullreturn;  
  62.    
  63.         //ScrollView中的唯一子控件的位置信息, 这个位置信息在整个控件的生命周期中保持不变  
  64.         originalRect.set(contentView.getLeft(), contentView.getTop(), contentView  
  65.                 .getRight(), contentView.getBottom());  
  66.     }  
  67.    
  68.     //在触摸事件中, 处理上拉和下拉的逻辑  
  69.     @Override  
  70.     public boolean dispatchTouchEvent(MotionEvent ev) {  
  71.            
  72.         if (contentView == null) {  
  73.             return super.dispatchTouchEvent(ev);  
  74.         }  
  75.    
  76.         int action = ev.getAction();  
  77.            
  78.         switch (action) {  
  79.         case MotionEvent.ACTION_DOWN:  
  80.                
  81.             //判断是否可以上拉和下拉  
  82.             canPullDown = isCanPullDown();  
  83.             canPullUp = isCanPullUp();  
  84.                
  85.             //记录按下时的Y值  
  86.             startY = ev.getY();  
  87.             break;  
  88.                
  89.         case MotionEvent.ACTION_UP:  
  90.                
  91.             if(!isMoved) break;  //如果没有移动布局, 则跳过执行  
  92.                
  93.             // 开启动画  
  94.             TranslateAnimation anim = new TranslateAnimation(00, contentView.getTop(),  
  95.                     originalRect.top);  
  96.             anim.setDuration(ANIM_TIME);  
  97.                
  98.             contentView.startAnimation(anim);  
  99.                
  100.             // 设置回到正常的布局位置  
  101.             contentView.layout(originalRect.left, originalRect.top,   
  102.                     originalRect.right, originalRect.bottom);  
  103.                
  104.             //将标志位设回false  
  105.             canPullDown = false;  
  106.             canPullUp = false;  
  107.             isMoved = false;  
  108.                
  109.             break;  
  110.         case MotionEvent.ACTION_MOVE:  
  111.                
  112.             //在移动的过程中, 既没有滚动到可以上拉的程度, 也没有滚动到可以下拉的程度  
  113.             if(!canPullDown && !canPullUp) {  
  114.                 startY = ev.getY();  
  115.                 canPullDown = isCanPullDown();  
  116.                 canPullUp = isCanPullUp();  
  117.                    
  118.                 break;  
  119.             }  
  120.                
  121.             //计算手指移动的距离  
  122.             float nowY = ev.getY();  
  123.             int deltaY = (int) (nowY - startY);  
  124.                
  125.             //是否应该移动布局  
  126.             boolean shouldMove =   
  127.                     (canPullDown && deltaY > 0)    //可以下拉, 并且手指向下移动  
  128.                     || (canPullUp && deltaY< 0)    //可以上拉, 并且手指向上移动  
  129.                     || (canPullUp && canPullDown); //既可以上拉也可以下拉(这种情况出现在ScrollView包裹的控件比ScrollView还小)  
  130.                
  131.             if(shouldMove){  
  132.                 //计算偏移量  
  133.                 int offset = (int)(deltaY * MOVE_FACTOR);  
  134.                    
  135.                 //随着手指的移动而移动布局  
  136.                 contentView.layout(originalRect.left, originalRect.top + offset,  
  137.                         originalRect.right, originalRect.bottom + offset);  
  138.                    
  139.                 isMoved = true;  //记录移动了布局  
  140.             }  
  141.                
  142.             break;  
  143.         default:  
  144.             break;  
  145.         }  
  146.    
  147.         return super.dispatchTouchEvent(ev);  
  148.     }  
  149.        
  150.    
  151.     //判断是否滚动到顶部  
  152.     private boolean isCanPullDown() {  
  153.         return getScrollY() == 0 ||   
  154.                 contentView.getHeight() < getHeight() + getScrollY();  
  155.     }  
  156.        
  157.     //判断是否滚动到底部  
  158.     private boolean isCanPullUp() {  
  159.         return  contentView.getHeight() <= getHeight() + getScrollY();  
  160.     }  
  161. }  
    代码注释非常清楚。
    布局文件直接使用
[html]  view plain  copy
  1. <com.wj.myreboundscrollview.customview.MyReboundScrollView  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     tools:context=".MainActivity"  
  7.     android:orientation="vertical">  
  8.   
  9.     <LinearLayout  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:orientation="vertical">  
  13.         <EditText  
  14.             android:id="@+id/et_name"  
  15.             android:layout_width="match_parent"  
  16.             android:layout_height="wrap_content"  
  17.             android:layout_marginTop="12dip"  
  18.             android:hint="Your Name"/>  
  19.   
  20.         <EditText  
  21.             android:id="@+id/et_feedback"  
  22.             android:layout_width="match_parent"  
  23.             android:layout_height="wrap_content"  
  24.             android:layout_marginTop="32dip"  
  25.             android:hint="Your Feedback"  
  26.             android:lines="5"/>  
  27.   
  28.         <Button  
  29.             android:id="@+id/btn_submit"  
  30.             android:layout_width="wrap_content"  
  31.             android:layout_height="wrap_content"  
  32.             android:layout_gravity="center"  
  33.             android:layout_marginTop="42dip"  
  34.             android:text="Submit"  
  35.             android:onClick="submit"/>  
  36.     </LinearLayout>  
  37. </com.wj.myreboundscrollview.customview.MyReboundScrollView>  
  这里直接在外层包裹实现。注意,因为Myreboundscrollview是继承自Scrollview,因此要遵循Scrollview的使用原则,里面只能包含一个LinearLayout,所以无论里面多门复杂的布局,最后我们都要将其包含在一个LinearLayout中。

  ok,功能实现,效果也演示,具体需要使用直接拿来用就可以。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值