1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
enum ZoomLevel {
LEVEL_1,
LEVEL_2,
LEVEL_3,
LEVEL_4
}
public class TouchWebView extends WebView {
private Point _scroll;
private float _scale;
private ZoomLevel _zoom;
//private static final float RATIO = 2.46f;
public TouchWebView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
_scroll = new Point(0,0);
_scale = 2.0f;
_zoom = ZoomLevel.LEVEL_2;
setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
view.setAlpha(0.1f);
}
@Override
public void onPageFinished(final WebView view, String url) {
Log.i("FINISHED", ":)");
view.scrollTo(_scroll.x, _scroll.y);
view.setAlpha(1f);
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
_scale = getScale(); //Pas beau, à corriger plus tard
if(getAlpha() < 1.0f) {
return false;
}
switch(event.getAction()) {
case MotionEvent.ACTION_MOVE:
if(event.getPointerCount() > 1) {
switch(_zoom) {
case LEVEL_2:
if(_scale >= 4.0) {
doZoom(ZoomLevel.LEVEL_3, 400, "file:///android_asset/zoom3.svg");
event.setAction(MotionEvent.ACTION_CANCEL);
}
break;
case LEVEL_3:
if(_scale < 2.5) {
doZoom(ZoomLevel.LEVEL_2, 250, "file:///android_asset/zoom2.svg");
event.setAction(MotionEvent.ACTION_CANCEL);
}
if(_scale > 6.5) {
doZoom(ZoomLevel.LEVEL_4, 650, "file:///android_asset/zoom4.svg");
event.setAction(MotionEvent.ACTION_CANCEL);
}
break;
case LEVEL_4:
if(_scale < 4.0) {
doZoom(ZoomLevel.LEVEL_3, 400, "file:///android_asset/zoom3.svg");
event.setAction(MotionEvent.ACTION_CANCEL);
}
break;
}
}
break;
}
return super.onTouchEvent(event);
}
private void doZoom(ZoomLevel level, int scale, String url) {
_scroll.x = (int) (getScrollX());
_scroll.y = (int) (getScrollY());
Log.i("SCROLL",_scroll.x + " / " + _scroll.y);
_zoom = level;
setInitialScale(scale);
loadUrl(url);
}
} |
Partager