WebView双击时的Scalefactor缩放量

时间:2013-11-07 14:56:40

标签: android webview android-canvas gesture

我正在WebView上实现画布,我需要根据WebView的缩放级别设置画布比例。将canvas.scale()SimpleOnScaleListener相结合可以解决问题。对于双击,我使用GestureDetector来检测何时进行双击并相应地设置比例因子。我只是猜测当双击时它会缩放多少:将比例因子设置为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;
    }

}

1 个答案:

答案 0 :(得分: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;
        }
    });
}