android 检测右滑的WebView

  1. import android.app.Activity;  
  2. import android.content.Context;  
  3. import android.util.AttributeSet;  
  4. import android.view.MotionEvent;  
  5. import android.view.VelocityTracker;  
  6. import android.view.View;  
  7. import android.view.View.OnTouchListener;  
  8. import android.webkit.WebSettings;  
  9. import android.webkit.WebSettings.RenderPriority;  
  10. import android.webkit.WebView;  
  11.   
  12. public class MyWebView extends WebView {  
  13.       
  14.       //手指向右滑动时的最小速度    
  15.     private static final int XSPEED_MIN = 200;    
  16.         
  17.     //手指向右滑动时的最小距离    
  18.     private static final int XDISTANCE_MIN = 150;    
  19.         
  20.     //记录手指按下时的横坐标。    
  21.     private float xDown;    
  22.         
  23.     //记录手指移动时的横坐标。    
  24.     private float xMove;    
  25.         
  26.     //用于计算手指滑动的速度。    
  27.     private VelocityTracker mVelocityTracker;  
  28.       
  29.     Context mContext;  
  30.   
  31.   
  32.     public MyWebView(Context context) {  
  33.         super(context);  
  34.         this.mContext = context;  
  35.         init();  
  36.     }  
  37.   
  38.     public MyWebView(Context context, AttributeSet attrs) {  
  39.         super(context, attrs);  
  40.         this.mContext = context;  
  41.         init();  
  42.     }  
  43.   
  44.     public MyWebView(Context context, AttributeSet attrs, int defStyle) {  
  45.         super(context, attrs, defStyle);  
  46.         this.mContext = context;  
  47.         init();  
  48.     }  
  49.   
  50.   
  51.     private void init() {  
  52.   
  53.         setScrollBarStyle(0);// 设置滚动条的宽度  
  54.         WebSettings webSettings = getSettings();  
  55.         webSettings.setJavaScriptEnabled(true);  
  56.         webSettings.setSaveFormData(false);  
  57.         webSettings.setSavePassword(false);  
  58.         webSettings.setSupportZoom(false);  
  59.         webSettings.setGeolocationEnabled(true);  
  60.         webSettings.setRenderPriority(RenderPriority.HIGH);  
  61.         webSettings.setSupportMultipleWindows(true);  
  62.         webSettings.setLoadsImagesAutomatically(true);  
  63.         webSettings.setUseWideViewPort(true);  
  64.     }  
  65.   
  66.   
  67.     @Override  
  68.        public boolean onTouchEvent(MotionEvent event) {    
  69.             createVelocityTracker(event);    
  70.             switch (event.getAction()) {    
  71.             case MotionEvent.ACTION_DOWN:    
  72.                 xDown = event.getRawX();    
  73.                 break;    
  74.             case MotionEvent.ACTION_MOVE:    
  75.                 xMove = event.getRawX();    
  76.                 //活动的距离    
  77.                 int distanceX = (int) (xMove - xDown);    
  78.                 //获取顺时速度    
  79.                 int xSpeed = getScrollVelocity();    
  80.                 //当滑动的距离大于我们设定的最小距离且滑动的瞬间速度大于我们设定的速度时,返回到上一个activity    
  81.                // if(distanceX > XDISTANCE_MIN && xSpeed > XSPEED_MIN) {    
  82.                 if(distanceX > XDISTANCE_MIN) {     
  83.                     return false;  
  84.                 }    
  85.                 break;    
  86.             case MotionEvent.ACTION_UP:    
  87.                 recycleVelocityTracker();    
  88.                 break;    
  89.             default:    
  90.                 break;    
  91.             }    
  92.             return super.onTouchEvent(event);    
  93.         }    
  94.             
  95.         /**  
  96.          * 创建VelocityTracker对象,并将触摸content界面的滑动事件加入到VelocityTracker当中。  
  97.          *   
  98.          * @param event  
  99.          *          
  100.          */    
  101.         private void createVelocityTracker(MotionEvent event) {    
  102.             if (mVelocityTracker == null) {    
  103.                 mVelocityTracker = VelocityTracker.obtain();    
  104.             }    
  105.             mVelocityTracker.addMovement(event);    
  106.         }    
  107.             
  108.         /**  
  109.          * 回收VelocityTracker对象。  
  110.          */    
  111.         private void recycleVelocityTracker() {    
  112.             mVelocityTracker.recycle();    
  113.             mVelocityTracker = null;    
  114.         }    
  115.             
  116.         /**  
  117.          * 获取手指在content界面滑动的速度。  
  118.          *   
  119.          * @return 滑动速度,以每秒钟移动了多少像素值为单位。  
  120.          */    
  121.         private int getScrollVelocity() {    
  122.             mVelocityTracker.computeCurrentVelocity(1000);    
  123.             int velocity = (int) mVelocityTracker.getXVelocity();    
  124.             return Math.abs(velocity);    
  125.         }  
  126. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值