如何使用翻转器启用多点触控缩放来进行 WebView 滑动手势检测

发布于 2024-09-09 21:26:37 字数 59 浏览 1 评论 0原文

那么,有没有一种方法可以让 webview 控件检测滑动,同时能够进行多点触控缩放并具有内置缩放控件呢?

So is there a way for a webview control to detect a swipe while capable of doing multitouch zoom and having build-in zoom controls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的旅程 2024-09-16 21:26:37

是的!有一种方法可以通过实现 WebView 并创建自定义 Webview 来做到这一点
这样自定义的 WebView 就内置了
滑动检测同时具有多点触控和内置控件
用于缩放。

//Declaring the custom Webview and put into a viewflipper


MyWebView[] webview =new MyWebView[2];
    flipper = (ViewFlipper) findViewById(R.id.ViewFlipper);

 webview[i] = new MyWebView(this);
 webview[i].setWebViewClient(new HelloWebViewClient());
 webview[i].getSettings().setJavaScriptEnabled(false);
 webview[i].setInitialScale(60); 
 webview[i].getSettings().setBuiltInZoomControls(true);

    flipper.addView(webview[0]);
    flipper.addView(webview[1]);

这是自定义的 webview

 public class MyWebView extends WebView {
  public MyWebView(Context context) {
   super(context);
  }



@Override 
     public boolean onTouchEvent(MotionEvent evt) {   

         boolean consumed = super.onTouchEvent(evt); 
     if (isClickable()) { 
         switch (evt.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
         lastTouchX = evt.getX(); 
         lastTouchY = evt.getY();
         downXValue = evt.getX();
         downTime = evt.getEventTime();
         hasMoved = false; 
         break; 
     case MotionEvent.ACTION_MOVE: 
         hasMoved = moved(evt); 
         break; 
     case MotionEvent.ACTION_UP: 
        float currentX = evt.getX();
           long currentTime = evt.getEventTime();
           float difference = Math.abs(downXValue - currentX);
           long time = currentTime - downTime;

           Log.i("Touch Event:",  "Distance: " + difference + "px Time: " + time + "ms");

           if ( (downXValue < currentX) && (time < 220) && (difference > 100) ) {
               go_back();
           }



             if ( (downXValue > currentX) && (time < 220) && (difference > 100) ) {
                   go_forward();


                  }

                 //if (!moved(evt)) performClick(); 
                 break; 
             } 
         } 
         return consumed || isClickable(); 
     } 
  float downXValue;
  long downTime;
     private float lastTouchX, lastTouchY; 
     private boolean hasMoved = false; 
     private boolean moved(MotionEvent evt) { 
         return hasMoved || 
             Math.abs(evt.getX() - lastTouchX) > 10.0 || 
             Math.abs(evt.getY() - lastTouchY) > 10.0; 
     }

 }

就是这样。您已经在滑动检测中构建了。代码有点“伪代码”并且还没有清理它,但是覆盖 MotionEvent.ACTION_MOVE 和 case MotionEvent.ACTION_UP 中的 onTouchEvent 应该可以解决问题。您还可以使用时间和差异范围。

YES!There is a way of doing that by implementing WebView and creating a custom Webview
This way the custom WebView has build in
swipe detection having at the same time multi touch and build in controls
for zoom.

//Declaring the custom Webview and put into a viewflipper


MyWebView[] webview =new MyWebView[2];
    flipper = (ViewFlipper) findViewById(R.id.ViewFlipper);

 webview[i] = new MyWebView(this);
 webview[i].setWebViewClient(new HelloWebViewClient());
 webview[i].getSettings().setJavaScriptEnabled(false);
 webview[i].setInitialScale(60); 
 webview[i].getSettings().setBuiltInZoomControls(true);

    flipper.addView(webview[0]);
    flipper.addView(webview[1]);

and here is the custom webview

 public class MyWebView extends WebView {
  public MyWebView(Context context) {
   super(context);
  }



@Override 
     public boolean onTouchEvent(MotionEvent evt) {   

         boolean consumed = super.onTouchEvent(evt); 
     if (isClickable()) { 
         switch (evt.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
         lastTouchX = evt.getX(); 
         lastTouchY = evt.getY();
         downXValue = evt.getX();
         downTime = evt.getEventTime();
         hasMoved = false; 
         break; 
     case MotionEvent.ACTION_MOVE: 
         hasMoved = moved(evt); 
         break; 
     case MotionEvent.ACTION_UP: 
        float currentX = evt.getX();
           long currentTime = evt.getEventTime();
           float difference = Math.abs(downXValue - currentX);
           long time = currentTime - downTime;

           Log.i("Touch Event:",  "Distance: " + difference + "px Time: " + time + "ms");

           if ( (downXValue < currentX) && (time < 220) && (difference > 100) ) {
               go_back();
           }



             if ( (downXValue > currentX) && (time < 220) && (difference > 100) ) {
                   go_forward();


                  }

                 //if (!moved(evt)) performClick(); 
                 break; 
             } 
         } 
         return consumed || isClickable(); 
     } 
  float downXValue;
  long downTime;
     private float lastTouchX, lastTouchY; 
     private boolean hasMoved = false; 
     private boolean moved(MotionEvent evt) { 
         return hasMoved || 
             Math.abs(evt.getX() - lastTouchX) > 10.0 || 
             Math.abs(evt.getY() - lastTouchY) > 10.0; 
     }

 }

And that's It.You have Build in swipe detection.Code is in a bit "pseudocode" and haven't cleaned it up but Overriding the onTouchEvent in MotionEvent.ACTION_MOVE and case MotionEvent.ACTION_UP should do the trick.You can also play with the time and difference bounds .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文