Back to project page adkintun-mobile-browser.
The source code is released under:
Apache License
If you think the Android project adkintun-mobile-browser listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * Copyright 2013 NIC Chile Research Labs * //from w ww . ja v a 2 s . co m * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cl.niclabs.adkintunmobile.browser; import android.content.Context; import android.graphics.PointF; import android.util.AttributeSet; import android.util.FloatMath; import android.view.MotionEvent; import android.view.View; import android.webkit.WebView; /** * Clase que implementa el zoom * */ public class CustomWebView extends WebView { private Context context; static final int NONE = 0; static final int DRAG = 1; static final int ZOOM = 2; int mode = NONE; PointF start = new PointF(); PointF mid = new PointF(); float oldDist = 1f; float scale = 0f; float oldscale = 0f; int displayHeight = 1; public CustomWebView(Context context) { super(context); this.context = context; this.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); this.getSettings().setUseWideViewPort(true); } public CustomWebView(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; this.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); this.getSettings().setUseWideViewPort(true); } public CustomWebView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; this.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); this.getSettings().setUseWideViewPort(true); } @Override public boolean onTouchEvent(MotionEvent ev) { boolean consumed = 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) { } else if (mode == ZOOM) { float newDist = 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(); } } } } //Topbar if((start.y - ev.getY()) < 0 && getScrollY()<=0){ ((MainActivity)context).topBarJustShown = true; ((MainActivity)context).showTopBar(); ((MainActivity)context).showNavigationButtons(); } break; } return consumed; } private float spacing(MotionEvent event) { float x = event.getX(0) - event.getX(1); float y = event.getY(0) - event.getY(1); return FloatMath.sqrt(x * x + y * y); } private void midPoint(PointF point, MotionEvent event) { float x = event.getX(0) + event.getX(1); float y = event.getY(0) + event.getY(1); point.set(x / 2, y / 2); } }