那么有一种方法可以让webview控件检测滑动,同时能够进行多点触控缩放并具有内置缩放控件吗?
是的!有一种方法可以通过实现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; } }
这就是It.You有一个刷卡检测.Code有点"伪代码"并且没有清理它但是覆盖MotionEvent.ACTION_MOVE中的onTouchEvent和案例MotionEvent.ACTION_UP应该可以解决问题.你也可以使用时间和差异的界限.