Android development implementation with rebound effect imitation iOS bounce ScrollView Tutorial detailed _android

Source: Internet
Author: User

First, let's take a look at the results of our final implementation today:

This is the rebound effect in iOS. Of course, if we want to achieve this effect in Android, it doesn't feel so stiff and rolls to the bottom or top. Of course

The use of ScrollView is not possible. So we need to create a new view inheritance ScrollView

Package Davidbouncescrollview.qq986945193.com.davidbouncescrollview;
Import Android.annotation.SuppressLint;
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; /** * @author: Programmer Xiaoice * @ Sina Weibo: http://weibo.com/mcxiaobing * @GitHub: https://github.com/QQ986945193 * @CSDN Blog: http://blog csdn.net/qq_21376985 * @ AC qq:986945193 * Class name: ScrollView with rebound effect * * public class Bouncescrollview extends ScrollView {priv Ate view inner;//child view private float y;//Click on the y-coordinate private Rect normal = new Rect ()//Rectangle (This is just a form, just to determine if animation is needed.) Private B Oolean Iscount = false;//Whether to start computing public bouncescrollview (context, AttributeSet attrs) {Super (context, attrs);}/ * Work is done based on the XML build view. The function is called at the end of the build view, after all child views have been added.
Even if the subclass overrides the Onfinishinflate * method, the method of the parent class should be called so that the method can be executed. * * @SuppressLint ("Missingsupercall") @Override protected void onfInishinflate () {if (Getchildcount () > 0) {inner = getchildat (0);}/*** * Listener Touch/@Override public boolean Ontou Chevent (motionevent ev) {if (inner!= null) {commontouchevent (EV);} return super.ontouchevent (EV);/*** * Touch Event * @p Aram EV/public void commontouchevent (motionevent ev) {int action = ev.getaction (), switch (action) {case motionevent.
Action_down:break;
Case MOTIONEVENT.ACTION_UP://Fingers loose.
if (Isneedanimation ()) {animation (); iscount = false;} break; /*** * Excludes the first move calculation because the y-coordinate is not known for the first time and is not available in Motionevent.action_down, * Because this is Myscrollview's touch event passed on to ListView's child item. So start with the second calculation. * However, we also have to initialize, that is, the first time we move the sliding distance to 0.
After the record is accurate, the normal execution. * https://github.com/QQ986945193/Case MotionEvent.ACTION_MOVE:final float prey = y;//press y-coordinate float Nowy = ev.gety ();
/time y coordinate int deltay = (int) (prey-nowy)//sliding distance if (!iscount) {deltay = 0;///here to 0.} y = nowy; When scrolling to the top or bottom of the scroll, then move the layout if (Isneedmove ()) {///Initialize the head rectangle if (Normal.isempty ()) {//Save the normal layout position normal.set (Inner.getlefT (), Inner.gettop (), Inner.getright (), Inner.getbottom ());
}//LOG.E ("JJ", "Rectangle:" + inner.getleft () + "," + inner.gettop ()//+ "," + inner.getright () + "," + Inner.getbottom ());
Mobile Layout Inner.layout (Inner.getleft (), Inner.gettop ()-DELTAY/2, Inner.getright (), Inner.getbottom ()-DELTAY/2);
} Iscount = true;
Break
Default:break; }/*** * Back-shrink animation/public void animation () {//Open mobile animation translateanimation ta = new translateanimation (0, 0, inner.gettop ()
, normal.top);
Ta.setduration (200);
Inner.startanimation (TA);
Set back to normal layout position inner.layout (Normal.left, Normal.top, Normal.right, Normal.bottom);
LOG.E ("JJ", "return:" + Normal.left + "," + Normal.top + "," + normal.right//+ "," + Normal.bottom);
Normal.setempty (); //Whether you need to turn on animation public boolean isneedanimation () {return!normal.isempty ();}/*** * need to move layout inner.getmeasuredheight (): Obtained Is the total height of the control * <p/> * getheight (): Gets the height of the screen * <p/> * https://github.com/QQ986945193 * * @return * * * Public Boolean I Sneedmove () {int offset = InnEr.getmeasuredheight ()-getheight ();
int scrolly = getscrolly ();
LOG.E ("JJ", "scrolly=" + scrolly);
0 is the top, the back one is the bottom if (scrolly = 0 | | scrolly = = offset) {return true;} return false; }
}

Then his usage is the same as the ScrollView usage. For example, refer directly to the layout:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" android:orientation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" Android: paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" Android :p addingtop= "@dimen/activity_vertical_margin" tools:context= " Davidbouncescrollview.qq986945193.com.davidbouncescrollview.MainActivity "> < Davidbouncescrollview.qq986945193.com.davidbouncescrollview.BounceScrollView android:layout_width= "Match_parent "android:layout_height=" match_parent "> <linearlayout android:layout_width=" match_parent "Android:layout_ height= "match_parent" android:orientation= "vertical" > <view android:layout_width= "match_parent" Android: layout_height= "1DP" android:background= "@android: Color/black"/> <textView android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "Center_" Horizontal "android:text=" Hello everyone, I'm a programmer Xiaoice "/> <view android:layout_width=" match_parent "android:layout_height=" 1DP "android:background=" @android: Color/black "/> <textview android:layout_width=" Wrap_content "Android:
layout_height= "Wrap_content" android:layout_gravity= "Center_horizontal" android:text= "Everybody good, I am programmer Xiaoice"/> <View
Android:layout_width= "Match_parent" android:layout_height= "1DP" android:background= "@android: Color/black"/> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Center_horizontal "android:text=" Everyone good, I am programmer Xiaoice "/> <view android:layout_width=" match_parent "Android:layout_"
height= "1DP" android:background= "@android: Color/black"/> <textview android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_gravity= "Center_horizontal" android:text= "Everybody good, I am programmer Xiaoice"/&GT <view android:layout_width= "match_parent" android:layout_height= "1DP" android:background= "@android: Color/black "/> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_" Gravity= "Center_horizontal" android:text= "Hello everyone, I am programmer Xiaoice"/> <view android:layout_width= "Match_parent" Android: layout_height= "1DP" android:background= "@android: Color/black"/> <textview android:layout_width= "Wrap_ Content "android:layout_height=" wrap_content "android:layout_gravity=" Center_horizontal "android:text=" everyone good, I'm a programmer Xiaoice "/> <view android:layout_width=" match_parent "android:layout_height=" 1DP "android:background=" @
Android:color/black "/> <textview android:layout_width= wrap_content" android:layout_height= "Wrap_content" Android:layout_gravity= "Center_horizontal" android:text= "Hello everyone, I am programmer Xiaoice"/> <view android:layout_width= "Match_ Parent "android:layout_height=" 1DP "android:background=" @android: Color/black "/> <textview android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "Center_horizontal" Android: text= "Hello, I'm a programmer Xiaoice"/> </LinearLayout> </ Davidbouncescrollview.qq986945193.com.davidbouncescrollview.bouncescrollview> </LinearLayout>

Finally run directly to see the above effect.

(Androidstudio version) GitHub download Address:

Https://github.com/QQ986945193/DavidBounceScrollView

The above is a small set to introduce the Android development with rebound effect of imitation iOS rebound ScrollView tutorial detailed, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.