如何知道webview在Android的画布上绘图
How to know webview isDrawing on Canvas in Android
首先,我的应用是一个混合型应用,我可以知道webview事件和动作通过Cordova Plugin原生。在WebView中,我们已经启用了滑动到下一页/上一页的手势检测器。
问题是当我在webview上画一条直线时,它会滑动到下一页。我不想滑动到下一页。所以,我需要禁用滑动期间webview isDrawing()
在画布或webview isZoomed()
。
这是我的CustomWebView,
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import org.apache.cordova.engine.SystemWebView;
public class CustomWebView extends SystemWebView {
private GestureDetector gestureDetector;
public CustomWebView(Context context) {
super(context);
}
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/*
* @see android.webkit.WebView#onScrollChanged(int, int, int, int)
*/
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
}
/*
* @see android.webkit.WebView#onTouchEvent(android.view.MotionEvent)
*/
@Override
public boolean onTouchEvent(MotionEvent ev) {
return gestureDetector.onTouchEvent(ev) || super.onTouchEvent(ev);
}
public void setGestureDetector(GestureDetector gestureDetector) {
this.gestureDetector = gestureDetector;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressWarnings("deprecation")
public boolean isZoomed () {
boolean result = false;
int contentHeight = getContentHeight();
int viewHeight = getHeight();
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR1 ) {
float scale = getScale();
int temp = (int)(((float)viewHeight / (float)contentHeight) * 100);
if (temp < (int)(scale * 100)) {
result = true;
}
} else {
float scale = getScale();
int temp = (int)(((float)viewHeight / (float)contentHeight) * 100);
if (temp < (int)(scale * 100)) {
result = true;
}
}
return result;
}
}
这是我的gestredetector,
private class CustomeGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if(e1 == null || e2 == null) return false;
if(e1.getPointerCount() > 1 || e2.getPointerCount() > 1) return false;
else {
try { // right to left swipe .. go to next page
if(e1.getX() - e2.getX() > 250 && Math.abs(velocityX) > 800) {
slideToLeft();
MoveNext();
}
return true;
} //left to right swipe .. go to prev page
else if (e2.getX() - e1.getX() > 250 && Math.abs(velocityX) > 800) {
slideToRight();
MovePrevious();
return true;
}
} catch (Exception e)
{
e.printStackTrace();
}
return false;
}
}
}
任何帮助将非常感激。谢谢!
你可以监听html画布上的触摸事件,并通知GestureDetector
"暂停"。
为了做到这一点,你需要在WebView
上打开一个javascript接口,如:
public class CustomWebView extends SystemWebView {
private CustomeGestureDetector gestureDetector;
public CustomWebView(Context context) {
super(context);
this.addJavascriptInterface(this, "Android");
}
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
this.addJavascriptInterface(this, "Android");
}
...
@JavascriptInterface
public void onCanvasTouchStart() {
this.gestureDetector.pause();
}
@JavascriptInterface
public void onCanvasTouchEnd() {
this.gestureDetector.resume();
}
}
private class CustomeGestureDetector extends GestureDetector.SimpleOnGestureListener {
private AtomicBoolean paused = new AtomicBoolean(false);
public void pause() {
this.paused.set(true);
}
public void resume() {
this.paused.set(false);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (this.paused.get()) {
return;
}
...
}
}
然后在html/javascript部分:
var canvas = document.getElementById("CANVAS_ID");
canvas.addEventListener("touchstart", function() { Android.onCanvasTouchStart(); });
canvas.addEventListener("touchend", function() { Android.onCanvasTouchEnd(); });
我还没有测试过这些,但总的想法应该是可行的。
相关文章:
- Canvas Html5绘图应用程序,移动画布会导致重大问题
- 如何在android中使用phonegap将文件从一个文件夹移动/复制到另一个文件夹
- 使用电话间隙在Android应用程序中显示SQL Lite的数据
- 如何在webView,Android中从@JavascriptInterface方法调用Javascript
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- 使用Native Sockets在Android中实现WebSockets
- window.onload没有'无法在Android WebView中工作
- cordova-2.0.0.js不适用于使用phonegap的android
- HTML画布在绘图后立即擦除矩形
- 如何在Ionic Android中将Javascript注入到web视图中
- 如何在JS Leatflet绘图插件中获取圆的坐标
- android XWalkView内容宽度
- AddEventListener适用于浏览器,但不适用于Android
- HTML锚标记无法在android平台中下载文件
- 在phonegap中为android调用onload函数的最佳方式
- Phonegap css height%不适用于android 4.2-4.3
- 在cordova android应用程序中连接数据库
- Android上URL方案的替代方案
- Android Webview通过Javascript注入CSS
- 如何知道webview在Android的画布上绘图