Android listview iPhone-like spring Effects

Source: Internet
Author: User

The IOS common view has an auto-return animation effect. Android has implemented a color gradient animation effect starting from 2.3 when it is pushed down to the bottom and pushed up to the bottom. The View class has two important methods: scrollto and scrollby, allowing you to move the visible area of the view. This is the theoretical basis for implementing the animation effect today. We can drag down the list or the corresponding mobile data list at the top of the android data list, and let it play back when the drag is stopped. The following is the code:
 
Package com. Linghu;
 
Import Android. content. context;
Import Android. util. attributeset;
Import Android. View. motionevent;
Import Android. widget. listview;

Public class pull_listview extends listview implements runnable {
Private float mlastdowny = 0f;
Private int mdistance = 0;
Private int mstep = 10;
Private Boolean mpositive = false;
 

Public pull_listview (context, attributeset attrs ){
Super (context, attrs );
}
 
Public pull_listview (context, attributeset attrs, int defstyle ){
Super (context, attrs, defstyle );
}
 
Public pull_listview (context ){
Super (context );
}
 
@ Override
Public Boolean ontouchevent (motionevent event ){
Switch (event. getaction ()){
Case motionevent. action_down:
If (mlastdowny = 0f & mdistance = 0 ){
Mlastdowny = event. Gety ();
Return true;
}
Break;
 
Case motionevent. action_cancel:
Break;

Case motionevent. action_up:
If (mdistance! = 0 ){
Mstep = 1;
Mpositive = (mdistance> = 0 );
This. Post (this );
Return true;
}
Mlastdowny = 0f;
Mdistance = 0;
Break;
 
Case motionevent. action_move:
If (mlastdowny! = 0f ){
Mdistance = (INT) (mlastdowny-event. Gety ());
If (mdistance <0 & getfirstvisibleposition () = 0 & getchildat (0 ). gettop () = 0) | (mdistance> 0 & getlastvisibleposition () = getcount ()-1 )){
Mdistance/= 2;
Scrollto (0, mdistance );
Return true;
}
}
Mdistance = 0;
Break;
}
Return super. ontouchevent (event );
}
 
@ Override
Public void run (){
Mdistance + = mdistance> 0? -Mstep: mstep;
Scrollto (0, mdistance );
If (mpositive & mdistance <= 0) | (! Mpositive & mdistance> = 0 )){
Scrollto (0, 0 );
Mdistance = 0;
Mlastdowny = 0f;
Return;
}
Mstep + = 1;
This. postdelayed (this, 10 );
}
} // End class
 

The above is a relatively old code, and the effect may not be very good at all. See the following: the effect is very smooth.

Address: http://download.csdn.net/detail/linghu_java/5899421

Related Article

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.