Android imitation QQ rebound damping ScrollView

Source: Internet
Author: User

Imitation QQ write a can play back and forth ScrollView.

You only need to rewrite ScrollView:

public class myscrollview extends scrollview {    //  The previous record position of the current touch point in the y direction     private int previousY = 0;    Start record position of touch point on  // y direction     private int startY = 0;   Touch point on   // y direction current record position     private int currentY = 0;  Relative distance between movement of two moves in    // y direction     private int deltaY = 0;     //  First sub-view     private View childView;     //  used to record the initial position of the Childview     private rect toprect = new  rect ();     public myscrollview (Context context)  {         super (context);    }    public  Myscrollview (CONTEXT&NBSP;CONTEXT,&NBSp Attributeset attrs)  {        super (context, attrs);     }    public myscrollview (context context,  Attributeset attrs, int defstyle)  {        super ( Context, attrs, defstyle);    }     @SuppressLint (" Missingsupercall ")      @Override     protected void  Onfinishinflate ()  {        if  (Getchildcount ()  >  0)  {            childView =  Getchildat (0);        }    }      @Override     public boolean dispatchtouchevent (motionevent event)  {        if  (Null == childview)  {             return super.dispatchtouchevent (event);         }         switch  (Event.getaction ())  {             case MotionEvent.ACTION_DOWN:                 startY =  (int)   Event.gety ();                 previousy = starty;                 break;            case  motionevent.action_move:                 currentY =  (int) &nbsP;event.gety ();                 deltaY = previousY - currentY;                 previousY = currentY;                 if  (0 == getscrolly ()                           | |  childview.getmeasuredheight ()  - getheight ()  <= getscrolly ())  {                     //   Record the initial position of the Childview                      if  (Toprect.isempty ())  {                         Toprect.set (Childview.getleft (),  childview.gettop (),                                  childview.getright (),  childview.getbottom ());                     }                     //   Update the location of Childview                      childview.layout (Childview.getleft (),  childview.gettop ()                                       - deltay / 3,  Childview.getright (),                             childview.getbottom () &NBSP;-&NBSP;DELTAY&NBSP;/&NBSP;3);                 }                 break;            case  motionevent.action_up:                 if  (!toprect.isempty ())  {                     updownmoveanimation ();                     //  child controls back to initial position                      childview.layout (topRect.left,  toprect.top, toprect.right,                             toprect.bottom);                 }                 startY = 0;                 currentY  = 0;                 Toprect.setempty ();                 break;            default:                 break;        }         return super.dispatchtouchevent (Event);     }    //  animation effect of the top and bottom rebound     private void  Updownmoveanimation ()  {        translateanimation animation  = new translateanimation (0.0f, 0.0f,                 childview.gettop (),  toprect.top);         animation.setduration (;        ) Animation.setinterpolator (New accelerateinterpolator ());         Childview.setanimation (animation);    }} 

Then you can use the direct layout. This overriding method does not conflict with the click events of the child controls.

This article is from the "Android Development column" blog, be sure to keep this source http://liuyvhao.blog.51cto.com/11690759/1836527

Android imitation QQ rebound damping ScrollView

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.