Initiate zoom within an Android WebView from a longpress
-
04-06-2021 - |
Вопрос
I am looking for a way to replicate the native WebView zoom on double-tap but triggered via a long press instead. As, with the double-tab zoom I'd like the zoom to scale centered around the point of touch and then be able to scale back out to the default zoom level programmatically.
I was thinking there might be a way to spoof the double-tap gesture to the WebView within the longpress handler and provide it the coordinates of the long press but I am unable to find a way to do it.
Can this be done?
Решение
Found a workable if a bit of a hacky solution to this based upon information found in this post:
Double tap Programmatically on WebView
I replaced the content of:
public void simulateDoubleTap()
{
simulateDoubleTapEvent(0);
simulateDoubleTapEvent(2);
simulateDoubleTapEvent(2);
simulateDoubleTapEvent(1);
simulateDoubleTapEvent(0);
}
With:
public void simulateDoubleTap()
{
simulateDoubleTapEvent(MotionEvent.ACTION_DOWN);
simulateDoubleTapEvent(MotionEvent.ACTION_UP);
simulateDoubleTapEvent(MotionEvent.ACTION_DOWN);
simulateDoubleTapEvent(MotionEvent.ACTION_UP);
}
And then added a call to simulateDoubleTap() from within my longpress handler. This seemed to work - but it was not 100% consistent. May want to play with adding some small delays between the events and see if that smooths it out. Also I noticed that when zooming in, things would work with a single event, but it was taking two triggers of the double-tap to zoom back out. This may be because of the gesture handler I am using, or some other issue, but generally this seems to do what I need.