Android Learning Scroller (ii)

Source: Internet
Author: User

Mainactivity as follows:

Package Cc.testscroller1;import Android.app.activity;import Android.content.context;import Android.graphics.Canvas ; Import Android.graphics.color;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.linearlayout;import Android.widget.linearlayout.layoutparams;import android.widget.scroller;/** * Demo Description: * Learn Scroller Class (principle) * * Principle Analysis: * Click b The background of the Utton will change, and the button will call invalidate () to redraw the request, which is the source of the view system redraw, the trigger of the scroll dynamic effect. * Now that the redraw request has been issued, the entire view system will have a top-down drawing. * So first outerlinearlayout redraw, then innerlinearlayout, and finally the custom button. * But Scrollto () is executed in Innerlinearlayout, which causes the button to redraw. * So it starts to redraw again, and the process goes on until Mscroller.computescrolloffset () * returns FALSE. * * * Test Instructions: * The 149th and 185 lines can be commented separately when testing * * * NOTE: * 1 about method Startscroll (int startX, int starty, int dx, int dy, int durat    ION) * First parameter: start sliding x Position * Second parameter: start sliding y position * Third parameter: Distance in x direction, positive left, negative right * fourth parameter: distance sliding in y direction, positive upward, negative number down * Fifth parameter: Sliding duration * * This method is just a scroll behavioral model (see Resources 6). It just sets some * properties for scroll. Sometimes the name of the method is seen Startscroll () thought it was used to control and start the swipe. * Not really, the real thing to swipe is the following method Scrollto (int x, int y). * Although it is not intended to actually slide, it can be used elsewhere to get the properties set in the method. * such as MSCROLLER.GETCURRX () or mscroller.getduration () and so on * 2 about method Scrollto (int x, int y) * Set the scrolled position  of your view * It is the option to slide the view to the specified location for display * * 3 about Method Computescrolloffset () * If It returns true, the animation is not yet  Finished. * The return value is Boolean type true indicates that scrolling has not completed, false indicates scrolling completed * This method is usually put in computescroll () to determine whether scrolling is over * * * Reference: * 1 http://blog.csdn. net/gemmem/article/details/7321910 * 2 http://gundumw100.iteye.com/blog/1884373 * 3 http://ipjmc.iteye.com/blog/ 1615828 * 4 http://blog.csdn.net/c_weibin/article/details/7438323 * 5 http://www.cnblogs.com/wanqieddy/archive/2012/ 05/05/2484534.html * 6 http://blog.csdn.net/hudashi/article/details/7353075 * * Thank you very much */public class Main Activity extends Activity {private Outerlinearlayout mouterlinearlayout;private innerlinearlayout MinnerlinearlaYout1;private Innerlinearlayout MInnerLinearLayout2;    Private Scroller Mscroller;                @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Mscroller = new Scroller (this);        Outerlinearlayout is the outermost linearlayout mouterlinearlayout = new Outerlinearlayout (this);        Mouterlinearlayout.setorientation (linearlayout.vertical);            Layoutparams outerlinearlayoutlayoutparams = new Layoutparams (layoutparams.fill_parent,layoutparams.fill_parent);                This.setcontentview (Mouterlinearlayout, outerlinearlayoutlayoutparams);        Innerlinearlayout the first linearlayout of the inner layer mInnerLinearLayout1 = new Innerlinearlayout (this);        Minnerlinearlayout1.setbackgroundcolor (Color.parsecolor ("#ff0033"));             Layoutparams innerLinearLayoutLayoutParams1 = new Layoutparams (layoutparams.fill_parent,layoutparams.fill_parent);      Innerlinearlayoutlayoutparams1.weight=1;  Mouterlinearlayout.addview (MINNERLINEARLAYOUT1,INNERLINEARLAYOUTLAYOUTPARAMS1);        Innerlinearlayout the second linearlayout of the inner layer mInnerLinearLayout2 = new Innerlinearlayout (this);        Minnerlinearlayout2.setbackgroundcolor (Color.parsecolor ("#0000cc"));             Layoutparams innerLinearLayoutLayoutParams2 = new Layoutparams (layoutparams.fill_parent,layoutparams.fill_parent);        Innerlinearlayoutlayoutparams2.weight=1;                Mouterlinearlayout.addview (MINNERLINEARLAYOUT2,INNERLINEARLAYOUTLAYOUTPARAMS2);        Add a button Buttonsubclass ButtonSubClass1 = new Buttonsubclass (this) for the first linearlayout;        Buttonsubclass1.settext ("Button1 in LinearLayout1");               Buttonsubclass1.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {                See note description above 1 mscroller.startscroll (20, 20,-200,-200, 1000);        }        }); Minnerlinearlayout1.addview (BUTTONSUBCLASS1);        Add a button Buttonsubclass ButtonSubClass2 = new Buttonsubclass (this) for the second linearlayout;        Buttonsubclass2.settext ("Button2 in LinearLayout2");                Buttonsubclass2.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {                See note description above 1 mscroller.startscroll (20, 20,-200,-200, 1000);        }        });            Minnerlinearlayout2.addview (BUTTONSUBCLASS2); }class Outerlinearlayout extends LinearLayout {public outerlinearlayout (context context) {super (context);} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); System.out.println ("Outerlinearlayout---> OnDraw ()");} @Overridepublic void Computescroll () {//See note description above 3if (Mscroller.computescrolloffset ()) {//See note description above 2scrollTo ( Mscroller.getcurrx (), Mscroller.getcurry ()); System.out.println ("Outerlinearlayout---> Computescroll ()")//system.out.println ("InnerlineArlayout computescroll ()---> currx () = "//+mscroller.getcurrx () +", curry= "+mscroller.getcurry () + ", StartX () =" +mscroller.getstartx () + ", starty=" +mscroller.getstarty ());}} @Overrideprotected void Dispatchdraw (canvas canvas) {super.dispatchdraw (canvas); System.out.println ("Outerlinearlayout--->dispatchdraw ()");}} Class Innerlinearlayout extends LinearLayout {public innerlinearlayout (context context) {super (context);} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); System.out.println ("Innerlinearlayout---> OnDraw ()");} @Overridepublic void Computescroll () {//See note description above 3if (Mscroller.computescrolloffset ()) {//See note description above 2scrollTo ( Mscroller.getcurrx (), Mscroller.getcurry ()); System.out.println ("Innerlinearlayout---> Computescroll ()")//system.out.println ("Innerlinearlayout                   Computescroll ()---> currx () = "//+mscroller.getcurrx () +", curry= "+mscroller.getcurry () + ", StartX () =" +mscroller.getstaRtX () + ", starty=" +mscroller.getstarty ());}}} Class Buttonsubclass extends Button {public Buttonsubclass (context context) {super (context);} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); System.out.println ("Buttonsubclass---> OnDraw ()");}}


Main.xml as follows:

<relativelayout 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 " ></RelativeLayout>


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.