Android imitation iOS rebound effect ScrollView rebound summary
Should the requirements of the project need to imitate the effect of iOS pull back, I searched the internet a lot of the most of it is to take scrollview change it
I tried some discoveries, and there was a little problem.
The following code is I have made a little change to the release of the people feel not too much trouble
Package Com.example.myscrollview;import Android.content.context;import Android.graphics.rect;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;import Android.view.animation.translateanimation;import android.widget.scrollview;/** * bolg:http://blog.csdn.net/ aaawqqq?viewmode=contents * * @author Baozi * */public class Myscrollview extends ScrollView {//drag the distance size = 4 means only allow drag Dynamic screen 1/4private static final int size = 4;private View inner;private float y;private rect normal = new Rect (); Public Myscrollview (Context context) {super (context);} Public Myscrollview (context context, AttributeSet Attrs) {Super (context, attrs);} @Overrideprotected void Onfinishinflate () {if (Getchildcount () > 0) {inner = getchildat (0);}} @Overridepublic boolean ontouchevent (motionevent ev) {if (inner = = null) {return super.ontouchevent (EV);} else {Commontou Chevent (EV);} return super.ontouchevent (EV);} public void commontouchevent (motionevent ev) {int action = ev.getaction ();Switch (action) {Case motionevent.action_down:y = ev.gety (); Break;case MotionEvent.ACTION_UP:if (Isneedanimation ()) {/ /LOG.V ("Mlguitar", "'ll up and animation"); animation (); Break;case MotionEvent.ACTION_MOVE:final Float PreY = y;float nowy = ev.gety ();/** * Size=4 represents the distance dragged to the height of the screen */int Delta y = (int) (prey-nowy)/size;//scroll//Scrollby (0, deltay), y = nowy;//will not scroll when scrolling to the top or bottom, then move layout if (Isneedmove ()) {if (norm Al.isempty ()) {//Save normal layout position Normal.set (Inner.getleft (), Inner.gettop (), Inner.getright (), Inner.getbottom ()); return;} int yy = Inner.gettop ()-deltay;//Mobile Layout inner.layout (Inner.getleft (), YY, Inner.getright (), Inner.getbottom ()-DeltaY);} Break;default:break;}} Turn on animation move public void animation () {//Turn on mobile animation translateanimation ta = new translateanimation (0, 0, inner.gettop (), Normal.top ); Ta.setduration (+); inner.startanimation (TA);//set back to normal layout position inner.layout (Normal.left, Normal.top, Normal.right, Normal.bottom); Normal.setempty ();} Whether to turn on animation public boolean isneedanimation () {return!noRmal.isempty ();} Whether you need to move the layout public boolean isneedmove () {int offset = inner.getmeasuredheight ()-getheight (); int scrolly = getscrolly (); if (scrolly = = 0 | | scrolly = = offset) {return true;} return false;}} ┏┓┏┓//┏┛┻━━━┛┻┓//┃┃//┃━┃//┃┳┛┗┳┃//┃┃//┃┻┃//┃┃//┗━┓┏━┛//┃┃ God Beast Bless ┃┃ Code No bug! ┃┗━━━┓//┃┣┓//┃┏┛//┗┓┓┏━┳┓┏┛//┃┫┫┃┫┫//┗┻┛┗┻┛
Inside the code, this parameter is used to set the distance to drag.
Size= 4 means that the view will only follow the finger slide 1/4 of the distance
The size=3 represents the distance that follows the finger sliding 1/3
Other empathy
demo:http://download.csdn.net/detail/aaawqqq/7629533
If there's a better effect, please ask me.
I wish you every day to write good code ...