Android self-defined component Family "2"--scroller class

Source: Internet
Author: User
Tags getcolor

In the previous article introduced the view class of Scrollto and Scrollby two methods, the two methods are not very familiar with the friends can first look at "their own definition of view and ViewGroup"

Scrollto and Scrollby, while achieving the offset of the view, did not have a better control of the moving process, the movement is instantaneous. The Scroller class is designed to solve problems.

Open the Scroller source code, you can see the Startscroll method:

    /** * Start Scrolling by providing a starting point and the distance to travel. * * @param startX starting horizontal scroll offset in pixels.     Positive * Numbers would scroll the content to the left. * @param starty starting vertical scroll offset in pixels.     Positive numbers * would scroll the content up. * @param dx horizontal distance to travel.     Positive numbers would scroll the * content to the left. * @param dy Vertical distance to travel.     Positive numbers would scroll the * content up.     * @param duration duration of the scroll in milliseconds.        */public void startscroll (int startX, int starty, int dx, int dy, int duration) {mmode = Scroll_mode;        Mfinished = false;        mduration = Duration;        Mstarttime = Animationutils.currentanimationtimemillis ();        Mstartx = StartX;        Mstarty = Starty;        MFINALX = StartX + dx;        Mfinaly = Starty + dy;     Mdeltax = DX;   Mdeltay = dy;    Mdurationreciprocal = 1.0f/(float) mduration; }
As you can see, the effect of this method is to move the view from one start position to the target position by moving an animation over a given move offset and time. Here's another look at the Computescroll method provided by the view class

   /**     * Called by a, parent to request, a child update its values for     MSCROLLX * and mscrolly if necessary. This would typically be do if the child is     * animating a scroll using a {@link Android.widget.Scroller Scroller}     * Object.     *    /public void Computescroll () {    }

for easy control of the sliding screen, the Android framework provides the Computescroll () method to control the process. When you draw a view, the method is called in the Draw () procedure . in order to implement the offset control, it is necessary to overload the method in its own definition of view/viewgroup.

@Override  protected void Dispatchdraw (canvas canvas) {      ...            for (int i = 0; i < count; i++) {          final View child = Children[getchilddrawingorder (count, i)];          if (child.mviewflags & visibility_mask) = = VISIBLE | | child.getanimation ()! = null) {More              |= drawchild (canvas, C Hild, drawingtime);  }}} Protected Boolean drawchild (canvas canvas, View child, Long drawingtime) {      ...      Child.computescroll ();      ...  
Let's use the specific analysis of the Android Scroller class to provide us with the Code analysis

Package Com.example.testscrollto;import Android.app.activity;import Android.content.context;import Android.graphics.canvas;import Android.os.bundle;import Android.util.log;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.linearlayout;import Android.widget.scroller;public class Mainactivity extends Activity {private static final String TAG = "Testscrolleractivi Ty "; LinearLayout lay1, Lay2, lay0;private Scroller mscroller; @Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); mscroller = new Scroller (this), lay1 = new Mylinearlayout (this); lay2 = new Mylinearlayout (this); Lay1.setbackgroundcolor (This.getresources (). GetColor (Android. R.color.darker_gray)); Lay2.setbackgroundcolor (This.getresources (). GetColor (Android. R.color.white)); lay0 = new Contentlinearlayout (this); Lay0.setorientation (linearlayout.vertical); Linearlayout.layoutparams p0 = new Linearlayout.layoutparams (LINEARLAYOUT.LAYOUTPArams. fill_parent,linearlayout.layoutparams.fill_parent); This.setcontentview (Lay0, p0); Linearlayout.layoutparams P1 = new Linearlayout.layoutparams (LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);p 1.weight = 1;lay0.addview (Lay1, p1); Linearlayout.layoutparams P2 = new Linearlayout.layoutparams (LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);p 2.weight = 1;lay0.addview (Lay2, p2); MyButton btn1 = new MyButton (this); MyButton btn2 = new MyButton (this), Btn1.settext ("btn in Layout1"), Btn2.settext ("btn in Layout2"); Btn1.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {mscroller.startscroll (0, 0,-30,- 30, 50);}); Btn2.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Mscroller.startscroll (20, 20,-50 ,-50, 50);}); Lay1.addview (BTN1); Lay2.addview (BTN2);} Class MyButton extends Button {public MyButton (Context ctx) {super (CTX);} @Overrideprotected void OnDraw (canvas canvas) {Super.onDraw (canvas); LOG.D (TAG, this.tostring () + "onDraw------");}} Class Mylinearlayout extends LinearLayout {public mylinearlayout (Context ctx) {super (CTX);} @Override/** * Called by a, parent to request, child update its values for MSCROLLX * and mscrolly if necessary. This would typically be do if the child is * animating a scroll using a {@link Android.widget.Scroller Scroller} * Object . */public void Computescroll () {log.d (TAG, this.tostring () + "Computescroll-----------"); Mscroller.computescrolloffset ())//Assuming that Mscroller did not call Startscroll, this will return false. {//Because the call to the Computescroll function is an mylinearlayout instance,//So calling Scrollto will be the child of the instance, that is, the MyButton instance Scrollto (Mscroller.getcurrx (), 0); LOG.D (TAG, "Getcurrx =" + Mscroller.getcurrx ());//continue to redraw the system Getchildat (0). Invalidate ();}} Class Contentlinearlayout extends LinearLayout {public contentlinearlayout (Context ctx) {super (CTX);} @Overrideprotected void Dispatchdraw (canvas canvas) {LOG.D (TAG, "Contentview Dispatchdraw"); Super.dispatchdraw ( canvas);}}}


Click the above button, log output such as the following:


When you click on the button, the background changes, you need to redraw, then the button call invalidate method request redraw, this is the scroll dynamic effect of the trigger source, Scroller object instance is a package location and speed of the variable, startscroll ( The method is to set some member variables, and the only effect that is set is to cause the Mscroller.computescrolloffset () method to return True. The Mscroller.startscroll () method is called at the same time that the button is redrawn. At this point the Mscroller variable is set to a valid value.
The next process is view top-down drawing. The Drawchild method is called when the lay1 is drawn, and the Computescrll () method is run:

public void Computescroll () {log.d (TAG, this.tostring () + "Computescroll-----------"); Mscroller.computescrolloffset ())//Assuming that Mscroller did not call Startscroll, this will return false. {//Because the call to the Computescroll function is an mylinearlayout instance,//So calling Scrollto will be the child of the instance, that is, the MyButton instance Scrollto (Mscroller.getcurrx (), 0); LOG.D (TAG, "Getcurrx =" + Mscroller.getcurrx ());//continue to redraw the system Getchildat (0). Invalidate ();}}
Mscroller.computescrolloffset () does not return false until the time set in Startscroll is reached.






Android self-defined component Family "2"--scroller class

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.