android 檢測右滑的WebView

來源:互聯網
上載者:User

標籤:tip   ram   over   one   觸摸   回收   init   action   處理   

今天產品出新花樣非得要右滑。。。。檢測到右滑手勢後事件不做處理放在Activity中做對應的處理即可了。

import android.app.Activity;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.VelocityTracker;import android.view.View;import android.view.View.OnTouchListener;import android.webkit.WebSettings;import android.webkit.WebSettings.RenderPriority;import android.webkit.WebView;public class MyWebView extends WebView {  //手指向右滑動時的最小速度      private static final int XSPEED_MIN = 200;            //手指向右滑動時的最小距離      private static final int XDISTANCE_MIN = 150;            //記錄手指按下時的橫座標。      private float xDown;            //記錄手指移動時的橫座標。

private float xMove; //用於計算手指滑動的速度。 private VelocityTracker mVelocityTracker; Context mContext;public MyWebView(Context context) {super(context);this.mContext = context;init();}public MyWebView(Context context, AttributeSet attrs) {super(context, attrs);this.mContext = context;init();}public MyWebView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);this.mContext = context;init();}private void init() {setScrollBarStyle(0);// 設定滾動欄的寬度WebSettings webSettings = getSettings();webSettings.setJavaScriptEnabled(true);webSettings.setSaveFormData(false);webSettings.setSavePassword(false);webSettings.setSupportZoom(false);webSettings.setGeolocationEnabled(true);webSettings.setRenderPriority(RenderPriority.HIGH);webSettings.setSupportMultipleWindows(true);webSettings.setLoadsImagesAutomatically(true);webSettings.setUseWideViewPort(true);}@Override public boolean onTouchEvent(MotionEvent event) { createVelocityTracker(event); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: xDown = event.getRawX(); break; case MotionEvent.ACTION_MOVE: xMove = event.getRawX(); //活動的距離 int distanceX = (int) (xMove - xDown); //擷取順時速度 int xSpeed = getScrollVelocity(); //當滑動的距離大於我們設定的最小距離且滑動的瞬間速度大於我們設定的速度時,返回到上一個activity // if(distanceX > XDISTANCE_MIN && xSpeed > XSPEED_MIN) { if(distanceX > XDISTANCE_MIN) { return false; } break; case MotionEvent.ACTION_UP: recycleVelocityTracker(); break; default: break; } return super.onTouchEvent(event); } /** * 建立VelocityTracker對象。並將觸摸content介面的滑動事件增加到VelocityTracker其中。 * * @param event * */ private void createVelocityTracker(MotionEvent event) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); } /** * 回收VelocityTracker對象。 */ private void recycleVelocityTracker() { mVelocityTracker.recycle(); mVelocityTracker = null; } /** * 擷取手指在content介面滑動的速度。 * * @return 滑動速度,以每秒鐘移動了多少像素值為單位。 */ private int getScrollVelocity() { mVelocityTracker.computeCurrentVelocity(1000); int velocity = (int) mVelocityTracker.getXVelocity(); return Math.abs(velocity); }}



android 檢測右滑的WebView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.