android – 如何通过使用鳍状肢启用多点触控缩放来进行webview滑动手势检测
作者:互联网
那么有一种方法可以让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应该可以解决问题.你也可以使用时间和差异的界限.
标签:android,swipe,multi-touch 来源: https://codeday.me/bug/20190621/1257978.html