1

我正在 WebView 上实现画布,我需要根据 WebView 的缩放级别来缩放画布。canvas.scale()与 a结合可以解决SimpleOnScaleListener问题。对于双击,我正在使用 aGestureDetector来检测何时有双击并相应地设置比例因子。我只是在猜测双击时它的缩放比例:将比例因子设置为2.2f适用于 7 英寸平板电脑,但对于 10 英寸平板电脑来说它的缩放比例太大了。

无论如何以编程方式确定WebView在双击时放大了多少?我基本上需要画布与 WebView 同步缩放。

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.scale(mScaleFactor, mScaleFactor, 0, 0);
    canvas.drawPath(drawPath, drawPaint);
    invalidate();
}

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

        // Don't let the object get too small or too large.
        if(Deal.on == false) {
            mScaleFactor *= detector.getScaleFactor();
            mScaleFactor = Math.max(1f, Math.min(mScaleFactor, 30.0f));
            System.out.println("mScaleFactor: " + mScaleFactor);
        }
        invalidate();
        return true;
    }
}

public class DoubleTapListener extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onDoubleTap(MotionEvent e) {
       if (Deal.on == false) {
           if (mScaleFactor > 1.0f) {
               mScaleFactor = 1.0f;
           }else {
               mScaleFactor = 2.2f;
           }
       }
       return true;
    }

}
4

1 回答 1

0

您可以通过扩展来捕捉缩放变化WebViewClient

public MyWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setWebViewClient(new WebViewClient() {
        @Override
        public void onScaleChanged(WebView view, float oldScale, float newScale) {
            super.onScaleChanged(view, oldScale, newScale);
            currentScale = newScale;
        }
    });
}
于 2013-11-07T15:04:06.370 回答