A common ios view has an auto-rollback animation effect when it is pulled down (down) at the top or pulled up (up) at the bottom. The famous CM room and some mobile phone manufacturers are also competing to integrate this effect. Android starts from 2.3 to achieve the color gradient animation effect when the bottom and top to the bottom. This classic special effect in the iphone meets the needs of android fans to pursue the experience to a certain extent. Next we will discuss how to achieve this effect and how to implement taper.
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. So how can we know at the top and bottom? Please refer to the relevant information of ListView. The specific principle is explained in the code.
Java code
Package com. droidwolf;
Import android. content. Context;
Import android. util. AttributeSet;
Import android. view. MotionEvent;
Import android. widget. ListView;
// By droidwolf reprinted, please note
Public class IpListView extends ListView implements Runnable {
Private float mLastDownY = 0f;
Private int mDistance = 0;
Private int mStep = 10;
Private boolean mPositive = false;
Public IpListView (Context context, AttributeSet attrs ){
Super (context, attrs );
}
Public IpListView (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
}
Public IpListView (Context 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:
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 animation implemented by IpListView is just a simple rollback effect, and it does not implement the upper and lower punch effects. The specific implementation is left for you to complete ~~
Author "hemowolf"