So is there a way for a webview control to detect a swipe while capable of doing multitouch zoom and having build-in zoom controls?
Asked
Active
Viewed 7,229 times
5
-
Nothing wrong with answering your own question, but it would be better if you could pose a question and enter an answer rather than just putting everything in the question. This makes it easier for people to respond to your solution and to possible offer alternate solutions to your problem. – David Webb Jul 14 '10 at 10:20
-
typical it's not a question.. but i'll do it.It might be more obvious that way. – weakwire Jul 14 '10 at 10:25
1 Answers
5
YES!There is a way of doing that by implementing WebView and creating a custom Webview This way the custom WebView has build in swipe detection having at the same time multi touch and build in controls for zoom.
//Declaring the custom Webview and put into a viewflipper
MyWebView[] webview =new MyWebView[2];
flipper = (ViewFlipper) findViewById(R.id.ViewFlipper);
webview[i] = new MyWebView(this);
webview[i].setWebViewClient(new HelloWebViewClient());
webview[i].getSettings().setJavaScriptEnabled(false);
webview[i].setInitialScale(60);
webview[i].getSettings().setBuiltInZoomControls(true);
flipper.addView(webview[0]);
flipper.addView(webview[1]);
and here is the custom webview
public class MyWebView extends WebView {
public MyWebView(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent evt) {
boolean consumed = super.onTouchEvent(evt);
if (isClickable()) {
switch (evt.getAction()) {
case MotionEvent.ACTION_DOWN:
lastTouchX = evt.getX();
lastTouchY = evt.getY();
downXValue = evt.getX();
downTime = evt.getEventTime();
hasMoved = false;
break;
case MotionEvent.ACTION_MOVE:
hasMoved = moved(evt);
break;
case MotionEvent.ACTION_UP:
float currentX = evt.getX();
long currentTime = evt.getEventTime();
float difference = Math.abs(downXValue - currentX);
long time = currentTime - downTime;
Log.i("Touch Event:", "Distance: " + difference + "px Time: " + time + "ms");
if ( (downXValue < currentX) && (time < 220) && (difference > 100) ) {
go_back();
}
if ( (downXValue > currentX) && (time < 220) && (difference > 100) ) {
go_forward();
}
//if (!moved(evt)) performClick();
break;
}
}
return consumed || isClickable();
}
float downXValue;
long downTime;
private float lastTouchX, lastTouchY;
private boolean hasMoved = false;
private boolean moved(MotionEvent evt) {
return hasMoved ||
Math.abs(evt.getX() - lastTouchX) > 10.0 ||
Math.abs(evt.getY() - lastTouchY) > 10.0;
}
}
And that's It.You have Build in swipe detection.Code is in a bit "pseudocode" and haven't cleaned it up but Overriding the onTouchEvent in MotionEvent.ACTION_MOVE and case MotionEvent.ACTION_UP should do the trick.You can also play with the time and difference bounds .

weakwire
- 9,284
- 8
- 53
- 78
-
I tried this but it gives me a problem with zooming in/out. It resets the webview size when you pinch zoom back to it's original size. – MegaNairda Dec 12 '12 at 09:32