Today's products have to be right-sliding .... After the right swipe gesture is detected, the event is not processed and placed in the activity to do the appropriate processing.
Import Android.app.activity;import Android.content.context;import Android.util.attributeset;import Android.view.motionevent;import Android.view.velocitytracker;import Android.view.view;import Android.view.view.ontouchlistener;import Android.webkit.websettings;import Android.webkit.websettings.renderpriority;import Android.webkit.webview;public class MyWebView extends WebView {// The minimum speed when the finger is sliding to the right private static final int xspeed_min = 200; The minimum distance when the finger is sliding to the right private static final int xdistance_min = 150; Record the horizontal axis of the finger when it is pressed. private float Xdown; Record the horizontal axis of the finger when moving. private float Xmove; Used to calculate the speed of finger slippage. Private Velocitytracker Mvelocitytracker; Context Mcontext;public Mywebview (context context) {super (context); this.mcontext = Context;init ();} Public Mywebview (context context, AttributeSet Attrs) {Super (context, attrs); this.mcontext = Context;init ();} Public Mywebview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs,Defstyle); this.mcontext = Context;init ();} private void Init () {setscrollbarstyle (0);//Set scroll bar width websettings websettings = getsettings (); Websettings.setjavascriptenabled (True); Websettings.setsaveformdata (false); Websettings.setsavepassword (false); Websettings.setsupportzoom (false); websettings.setgeolocationenabled (true); Websettings.setrenderpriority ( Renderpriority.high); Websettings.setsupportmultiplewindows (true); Websettings.setloadsimagesautomatically (True) ; Websettings.setusewideviewport (true);} @Override public boolean ontouchevent (Motionevent event) {Createvelocitytracker (event); Switch (event.getaction ()) {Case MotionEvent.ACTION_DOWN:xDown = event.getrawx (); Break Case MotionEvent.ACTION_MOVE:xMove = EVENT.GETRAWX (); The distance of the activity int DISTANCEX = (int) (xmove-xdown); Get time-of-speed int xspeed = getscrollvelocity (); When the sliding distance is greater than the minimum distance we set and the instantaneous speed of the slide is greater than the speed we set, return to the previous activity//if (Distancex > Xdistance_min && xspeed > Xspeed_min) {if (Dista Ncex > Xdistance_min) {return false; } break; Case MotionEvent.ACTION_UP:recycleVelocityTracker (); Break Default:break; } return Super.ontouchevent (event); }/** * Creates a Velocitytracker object and joins the sliding events that touch the content interface into the Velocitytracker. * * @param event * */private void Createvelocitytracker (Motionevent event) {if (MV Elocitytracker = = null) {Mvelocitytracker = Velocitytracker.obtain (); } mvelocitytracker.addmovement (event); }/** * Reclaims Velocitytracker objects. */private void Recyclevelocitytracker () {mvelocitytracker.recycle (); Mvelocitytracker = null; /** * Gets the speed at which the finger slides in the content interface. * * @return sliding speed, in units of how many pixel values are moved per second. */private int getscrollvelocity () {mvelocitytracker.computecurrentvelocity (1000); int velocity = (int) mvelocitytracker.getxvelocity (); return math.abs (velocity); }}
Android detects right-sliding webview