如何控制webView中的缩放

时间:2011-11-08 10:58:42

标签: android webview android-webview zooming

我有这段代码:

[...]
{
    webView = (WebView) findViewById(R.id.webView1);
    webView.setInitialScale(getScale(Double.parseDouble(pubPages)));
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.setPadding(0, 0, 0, 0);

    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {
            activity.setTitle("Loading...");
            activity.setProgress(progress * 100);

            if(progress == 100)
                activity.setTitle(R.string.app_name);
        }
    });

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            // Handle the error
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }
    });

    webView.loadUrl("http://urltoshow");
}

private int getScale(double numPages){
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int screenHeight = dm.heightPixels;

    Double val = (new Double(screenHeight-40)/(new Double(1024)));    
    val = val * 100d;
    return val.intValue();
}

它工作正常,但我想禁止将其侧面缩小到自然高度以下。

放大是,缩小是,但最多可以。

怎么做?

3 个答案:

答案 0 :(得分:4)

 webview.getSettings().setBuiltInZoomControls(true);

答案 1 :(得分:3)

如果你想控制zoomin - 缩小然后不要使用默认的zoomin控件。 在您的xml文件集中..

<ZoomControls android:id="@+id/zoomctrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top|center_horizontal"
/>

现在。在您的活动中声明并初始化ZoomControls zc;

class CurOverlay extends Overlay {
    private GeoPoint pointToDraw;

    public void setPointToDraw(GeoPoint point) {
        pointToDraw = point;
    }

    public OnTouchListener mMapTouch = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.v("TAG", "inside onTouch");
            zoomlvl = curmapView.getZoomLevel();
            if (zoomlvl == 19)
                zc.setIsZoomInEnabled(false);
            else if (zoomlvl == 1)
                zc.setIsZoomOutEnabled(false);
            else {
                zc.setIsZoomInEnabled(true);
                zc.setIsZoomOutEnabled(true);
            }
            return false;
        }
    };
            ...
            ....

}

对于放大和缩小的点击事件是..

zc.setOnZoomInClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    zc.setIsZoomOutEnabled(true);
                    curmapController.zoomIn();
                    if (curmapView.getZoomLevel() == 19)
                        zc.setIsZoomInEnabled(false);

                }
            });
            zc.setOnZoomOutClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    zc.setIsZoomInEnabled(true);
                    curmapController.zoomOut();
                    if (curmapView.getZoomLevel() == 1)
                        zc.setIsZoomOutEnabled(false);
                }
            });

希望这张地图可以帮助你..

答案 2 :(得分:3)

为后代:

public class CustomWebView extends WebView {
    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    int mode = NONE;

    PointF start = new PointF();
    PointF mid = new PointF();
    float oldDist = 1f;
    float scale = 0f;
    float oldscale = 0f;
    int displayHeight;

    public CustomWebView(Context context) {
        super(context);
    }

    public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        boolean consumed = super.onTouchEvent(ev);

        if (isClickable())
            switch (ev.getAction() & MotionEvent.ACTION_MASK) {

               case MotionEvent.ACTION_DOWN: 
                  start.set(ev.getX(), ev.getY());
                  mode = DRAG;
                  break;
               case MotionEvent.ACTION_UP: 
               case MotionEvent.ACTION_POINTER_UP: 
                  mode = NONE;
                  break;
               case MotionEvent.ACTION_POINTER_DOWN: 
                  oldDist = spacing(ev);
                  if (oldDist > 5f) {
                     midPoint(mid, ev);
                     mode = ZOOM;
                  }
                  break;

               case MotionEvent.ACTION_MOVE: 
                  if (mode == DRAG) { 
                  }
                  else if (mode == ZOOM) { 
                     float newDist = spacing(ev);
                     if (newDist > 5f) {
                        scale = newDist / oldDist; 
                        if(scale>1){
                            if(Math.abs(oldscale-scale)>0.3){
                                zoomIn();
                                oldscale = scale;
                            }
                        }
                        System.out.println(scale);
                        if(scale<1){
                            if((getContentHeight()*getScale()>displayHeight)){
                                zoomOut();
                                System.out.println(getScale());
                            }
                        }
                     }
                  }
                  break;
               }
        return consumed;
    }

    private float spacing(MotionEvent event) {
           float x = event.getX(0) - event.getX(1);
           float y = event.getY(0) - event.getY(1);
           return FloatMath.sqrt(x * x + y * y);
        }

        private void midPoint(PointF point, MotionEvent event) {
           float x = event.getX(0) + event.getX(1);
           float y = event.getY(0) + event.getY(1);
           point.set(x / 2, y / 2);
        }
}

这项工作很好。