How to control zoom in webView

后端 未结 4 1119
孤城傲影
孤城傲影 2020-12-06 15:35

I have this code:

[...]
{
    webView = (WebView) findViewById(R.id.webView1);
    webView.setInitialScale(getScale(Double.parseDouble(pubPages)));
    webVi         


        
相关标签:
4条回答
  • 2020-12-06 15:55

    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.

    0 讨论(0)
  • 2020-12-06 16:01
     webview.getSettings().setBuiltInZoomControls(true);
    
    0 讨论(0)
  • 2020-12-06 16:02

    if you want to controll zoomin - zoom out then dont use default zoomin controll. In your xml file set..

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

    and now. in your activity Declare and initialize 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;
            }
        };
                ...
                ....
    
    }
    

    and for click event of zoom in and zoom out is ..

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

    hope this map help you ..

    0 讨论(0)
  • 2020-12-06 16:17
         FrameLayout mContentView = (FrameLayout) getWindow().
         getDecorView().findViewById(android.R.id.content);
         final View zoom = this.myWebView.getZoomControls();
         mContentView.addView(zoom, ZOOM_PARAMS);
         zoom.setVisibility(View.GONE);
    

    use this!!

    0 讨论(0)
提交回复
热议问题