如何知道webview在Android的画布上绘图

How to know webview isDrawing on Canvas in Android

本文关键字:绘图 Android 何知道 webview      更新时间:2023-09-26

首先,我的应用是一个混合型应用,我可以知道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(); });

我还没有测试过这些,但总的想法应该是可行的。