简体   繁体   English

在WebView Android上与Canvas缩放同步

[英]Scaling sync with Canvas on WebView Android

I'm having one issue with doing a Canvas on a WebView: Zooming. 我在WebView上做Canvas时遇到一个问题:缩放。 I'm using a GestureListener to get the scale factor and adjusting my canvas accordingly with canvas.scale() . 我正在使用GestureListener来获取比例因子,并使用canvas.scale()相应地调整画布。 This seems to work with gentle, simple pinch to zoom. 这似乎可以轻柔,简单地放大来工作。 Occasionally, however, if I try to pinch really quickly or place one finger down and rotate around it with another, the WebView and Canvas don't zoom equal amounts (not good). 但是,有时候,如果我尝试快速捏住手指或将一根手指放下并与另一根手指一起旋转,则WebView和Canvas不会缩放相同的大小(不好)。

A couple of possible solutions: 1) Is there a way to detect how much a WebView is zoomed in? 有两种可能的解决方案:1)是否可以检测WebView放大了多少? Or calculate it somehow, then set that scale to the canvas. 或以某种方式进行计算,然后将该比例设置为画布。 2) Set the zoom level of the WebView programmatically. 2)以编程方式设置WebView的缩放级别。 Any suggestions? 有什么建议么?

I've tried several things but nothing seems to get the canvas and WebView to sync nicely when zooming. 我尝试了几件事,但是似乎没有什么可以使画布和WebView在缩放时很好地同步。 The onScaleChanged for a WebViewClient only detects double tap, not pinch zooming. WebViewClient的onScaleChanged仅检测到双击,而不是捏缩放。 Here's my onDraw and GestureListener: 这是我的onDraw和GestureListener:

@Override
protected void onDraw(Canvas canvas) {
    canvas.save();
    super.onDraw(canvas);
    clipBounds = canvas.getClipBounds();
    drawPaint.setStrokeWidth(8/mScaleFactor);
    canvas.scale(mScaleFactor, mScaleFactor, 0, 0);
    for (int i = 0; i < colors.size(); i++) {
        canvas.drawPath(paths.get(i), colors.get(i));
    }
    canvas.drawPath(drawPath, drawPaint);
    canvas.drawBitmap(canvasBitmap, 0, 0, drawPaint);
    canvas.restore();
    invalidate();
}


public class ScaleGestureListener extends SimpleOnScaleGestureListener {                
    @Override
    public boolean onScale(ScaleGestureDetector detector) {

        if(Deal.on == false) {
            mScaleFactor *= detector.getScaleFactor();
            mScaleFactor = Math.max(1.0f, Math.min(mScaleFactor, 6.63f));
        }
        invalidate();
        return true;
    }

WebView settings: WebView设置:

WebSettings webSettings = drawView.getSettings(); WebSettings webSettings = drawView.getSettings();

    webSettings.setLoadWithOverviewMode(true);
    webSettings.setUseWideViewPort(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setSupportZoom(true);
    webSettings.setDisplayZoomControls(false);

    webSettings.setJavaScriptEnabled(true);

Calling the getScale method from onDraw should do what you need. 从onDraw调用getScale方法应该可以满足您的需求。 It has been marked as deprecated but if onScrollChanged doesn't work for you then I don't think you have any other options. 它已被标记为已弃用,但如果onScrollChanged对您不起作用,那么我认为您没有其他选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM