Android detects right-sliding webview

Source: Internet
Author: User

Today's products have to be right-sliding .... Detection to the right slip gesture after the event does not do processing in the activity to do the corresponding 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 = cont Ext;init ();} private void Init () {setscrollbarstyle (0);//sets the width of the scroll bar 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 (Distancex > Xdistance_min) {return false; } break; Case MotionEvent.ACTION_UP:recycleVelocityTracker (); Break Default:break; } return Super.ontouchevent (event); }/** * Create Velocitytracker object. and add the sliding events of the touch content interface to Velocitytracker. * * @param event * */private void Createvelocitytracker (Motionevent event) { if (Mvelocitytracker = = 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.