Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

android - How to control zoom in webView

I have this code:

[...]
{
    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();
}

And it works fine, but I want to disallow zooming the side below its natural height.

Zoom in yes, zoom out yes but up to a point.

How to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For future generations:

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);
        }
}

This work fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...