Android WebView Zooming using the ScaleDetector
HI,
Is it possible to support zooming of a WebView (similar to the default browser) when the user pinches and moves their fingers closer / further away. Im using 2.2 and tought this would be possible using the ScaleGestureDetector. THe problem is that I can't seem to find a method to set the scale. WebView.getScale() returns the current scale which changes through zoomIn() and ZoomOut() but I can'开发者_JAVA技巧t find setScale() or an option to set zooming by default and let the OS handle it.
Andy
Use:
webview.getSettings().setBuiltInZoomControls(true);
to do pinch zooming and put the default zoom in/out buttons on the screen.
You need to implement the onTouchEvent(MotionEvent) in your WebView.
Then in that event, pass the MotionEvent to the ScaleGestureDetector so that it can process it, then call the super.
class ScalableWebView extends WebView implements ScaleGestureDetector.OnScaleGestureListener
{
ScaleGestureDetector mScaleDetector = new ScaleGestureDetector(this, this);
public boolean onTouchEvent(MotionEvent event)
{
if(mScaleDetector.onTouchEvent(event))
return true;
return super.onTouchEvent(event);
}
boolean onScaleBegin(ScaleGestureDetector detector)
{
//handle scale event begin
}
boolean onScale(ScaleGestureDetector detector)
{
//handle scale event
}
boolean onScaleEnd(ScaleGestureDetector detector)
{
//handle scale event end
}
}
There is a setScale
, try this :
webView.setInitialScale((int)(100 * webView.getScale());
精彩评论