Android custom scrolling up/down rebound Scollview

Source: Internet
Author: User

This is a custom view that, in the XML layout, uses this view to nest the view to make it bounce back and down,

The layout can be rolled up and down, and the custom View code is as follows:

Package Com.loopfire.meitaotao.view.scrollview;import Android.content.context;import Android.graphics.Rect;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;import Android.view.animation.animation;import Android.view.animation.animation.animationlistener;import Android.view.animation.translateanimation;import android.widget.scrollview;/** * up and down rebound scollView * * @author Administrator * */public class Myscrollview extends ScrollView {private View inner;private float y;private Rect normal = New Rect ();p rivate boolean animationfinish = True;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 onintercepttouchevent (motionevent ev) {return super.onintercepttouchevent (EV);} @Overridepublic boolean ontouchevent (motionevent ev) {if (inner = = null) {return SUPer.ontouchevent (EV);} else {commontouchevent (EV);} return super.ontouchevent (EV);} private void Commontouchevent (Motionevent ev) {if (animationfinish) {int action = ev.getaction (); switch (action) {case Mot ionevent.action_down://System.out.println ("Action_down"); y = Ev.gety (); super.ontouchevent (EV); break;case motionevent.action_up://System.out.println ("action_up"); y = 0;if (isneedanimation ()) {animation ();} Super.ontouchevent (EV); Break;case motionevent.action_move://System.out.println ("Action_move"); final float PreY = y = = 0? Ev.gety (): Y;float nowy = ev.gety (); int deltay = (int) (prey-nowy);//scroll//Scrollby (0, deltay); y = nowy;//when scrolling to the top or bottom No more scrolling, then move layout if (Isneedmove ()) {if (Normal.isempty ()) {///save normal layout position Normal.set (Inner.getleft (), Inner.gettop (), Inner.getright (), Inner.getbottom ());} Mobile Layout Inner.layout (Inner.getleft (), Inner.gettop ()-DELTAY/2, Inner.getright (), Inner.getbottom ()-DELTAY/2);} else {super.ontouchevent (EV);} Break;default:break;}}} Turn on animation move private void Animation () {//Turn on mobile animation translateanimation ta = new translateanimation (0, 0, 0, normal.top-inner.gettop ()); Ta.setduration (200 ); Ta.setanimationlistener (new Animationlistener () {@Overridepublic void Onanimationstart (Animation Animation) { Animationfinish = false;} @Overridepublic void Onanimationrepeat (Animation Animation) {} @Overridepublic void Onanimationend (Animation Animation {inner.clearanimation ();//set back to normal layout position inner.layout (Normal.left, Normal.top, Normal.right, Normal.bottom); Normal.setempty (); animationfinish = True;}}); Inner.startanimation (TA);} Whether to turn on the animation private Boolean isneedanimation () {return!normal.isempty ();} Whether you need to move the layout private Boolean isneedmove () {int offset = inner.getmeasuredheight ()-getheight (); int scrolly = getscrolly (); I F (scrolly = = 0 | | scrolly = = offset) {return true;} return false;}}

In XML, use the following:

<com.loopfire.meitaotao.view.scrollView.MyScrollView>                        <textview                            style= "@style/form_left_text_ Style "                            android:layout_width=" wrap_content "                            android:layout_height=" wrap_content "                            android:layout_ marginleft= "@dimen/text_margin_left2"                            android:text= "@string/about"/>                    </ Com.loopfire.meitaotao.view.scrollview.myscrollview>

So the included TextView can scroll up and down and bounce back.

Android custom scrolling up/down rebound Scollview

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.