How To Control Zoom In Webview
I have this code: [...] { webView = (WebView) findViewById(R.id.webView1); webView.setInitialScale(getScale(Double.parseDouble(pubPages))); webView.getSettings().setJav
Solution 1:
For future generations:
publicclassCustomWebViewextendsWebView {
staticfinalintNONE=0;
staticfinalintDRAG=1;
staticfinalintZOOM=2;
intmode= NONE;
PointFstart=newPointF();
PointFmid=newPointF();
floatoldDist=1f;
floatscale=0f;
floatoldscale=0f;
int displayHeight;
publicCustomWebView(Context context) {
super(context);
}
publicCustomWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
publicCustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@OverridepublicbooleanonTouchEvent(MotionEvent ev) {
booleanconsumed=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) {
}
elseif (mode == ZOOM) {
floatnewDist= 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;
}
privatefloatspacing(MotionEvent event) {
floatx= event.getX(0) - event.getX(1);
floaty= event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
privatevoidmidPoint(PointF point, MotionEvent event) {
floatx= event.getX(0) + event.getX(1);
floaty= event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
}
This work fine.
Solution 2:
webview.getSettings().setBuiltInZoomControls(true);
Solution 3:
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;
classCurOverlayextendsOverlay {
private GeoPoint pointToDraw;
publicvoidsetPointToDraw(GeoPoint point) {
pointToDraw = point;
}
publicOnTouchListenermMapTouch=newOnTouchListener() {
@OverridepublicbooleanonTouch(View v, MotionEvent event) {
Log.v("TAG", "inside onTouch");
zoomlvl = curmapView.getZoomLevel();
if (zoomlvl == 19)
zc.setIsZoomInEnabled(false);
elseif (zoomlvl == 1)
zc.setIsZoomOutEnabled(false);
else {
zc.setIsZoomInEnabled(true);
zc.setIsZoomOutEnabled(true);
}
returnfalse;
}
};
...
....
}
and for click event of zoom in and zoom out is ..
zc.setOnZoomInClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
zc.setIsZoomOutEnabled(true);
curmapController.zoomIn();
if (curmapView.getZoomLevel() == 19)
zc.setIsZoomInEnabled(false);
}
});
zc.setOnZoomOutClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
zc.setIsZoomInEnabled(true);
curmapController.zoomOut();
if (curmapView.getZoomLevel() == 1)
zc.setIsZoomOutEnabled(false);
}
});
hope this map help you ..
Solution 4:
FrameLayoutmContentView= (FrameLayout) getWindow().
getDecorView().findViewById(android.R.id.content);
finalViewzoom=this.myWebView.getZoomControls();
mContentView.addView(zoom, ZOOM_PARAMS);
zoom.setVisibility(View.GONE);
use this!!
Post a Comment for "How To Control Zoom In Webview"