Sorting of custom ViewGroup originating from dream _ 1

Source: Internet
Author: User

Today, we will talk about custom controls, something that is a little more down-level. Today's main task is to completely write code and write a ViewGroup to implement a function similar to ViewPager.

User-Defined views must have been written, but not much is estimated. After work, all of them are custom views and viewgroups.

ViewPager can be used to achieve this sliding effect. In fact, ViewPager, ListView, SlidingMenu, or popupWindow all share the same thing, that is, the View (size) on the interface is unchanged, but the position is changing, as long as the position is changing, tongtong uses the same technology. Their underlying principles are the same. However, no matter how much you learn, these components will never end. At the beginning, learning these components is a kind of knowledge accumulation. However, when we learn more, we have to look back and think about how they are implemented. If we do it ourselves, how can we implement it. At work, the components of those systems may not meet our requirements.

   

One of the technical points is the scrollTo and scrollBy methods. The following Demo is used as an example.

 

The following control button can control the movement of the above View.

ScrollBy is a View method, which is an underlying method. Therefore, all the elements we see in Android have this method. What is this method for? The scrollBy method makes the View move a whole distance. For example, there is a viewGroup with many sub-views. If you want to change the position of these sub-views, what are the following methods? Either is to change the actual position of the View. However, if there are many sub-views in the ViewGroup, You need to modify each sub-View, this is implemented like listView. If I want to move the child View in the ViewGroup as a whole, I do not need to change every child View, instead, we can make an overall offset for them. Perform scrollBy on the ViewGroup so that all the sub-views in the ViewGroup are offset.

ScrollBy source code: (offset from the current position and then move (x, y)

/**

* Move the scrolled position of your view. This will cause a call

* {@ Link # onScrollChanged (int, int)} and the view will be

* Invalidated.

* @ Param x the amount of pixels to scroll by horizontally

* @ Param y the amount of pixels to scroll by vertically

*/

Public void scrollBy (int x, int y ){

ScrollTo (mScrollX + x, mScrollY + y );

}

ScrollTo source code: (move to (x, y)

/**

* Set the scrolled position of your view. This will cause a call

* {@ Link # onScrollChanged (int, int)} and the view will be

* Invalidated.

* @ Param x the x position to scroll

* @ Param y the y position to scroll

*/

Public void scrollTo (int x, int y ){

If (mScrollX! = X | mScrollY! = Y ){

Int oldX = mScrollX;

Int oldY = mScrollY;

MScrollX = x;

MScrollY = y;

InvalidateParentCaches ();

OnScrollChanged (mScrollX, mScrollY, oldX, oldY );

If (! AwakenScrollBars ()){

PostInvalidateOnAnimation ();

}

}

}

The two methods offset the content in the View, and the position of the View itself remains unchanged.

  

BaiHeTest example: (including the lyrics)

It is a sidebar sliding menu, which will definitely achieve this effect at work. Some may think of using the SlidingMenu open-source framework, and some may think of using Animation.

The Animation implementation method of Animation is characterized by only changing the effect we see without changing its real physical properties. Therefore, the physical location of the Button in this example remains unchanged. Animation can only be viewed as an Animation, rather than interactive effects. Therefore, using Animation cannot (accurately) Implement the sidebar menu.

Implementation of scrollTo and scrollBy methods:

Frame layout, which contains buttons. below is the side menu. Directly layout scrollTo to a position (-, 0), (negative value offset to the right), use a flag to determine whether it has been enabled. This can be achieved. If you want to see a similar animation effect, you can use the following two statements to replace scrollTo:

Scroller. startScroll (layout2.getScrollX (), layout2.getScrollY (),-200, 0 );

Handler. sendEmptyMessage (FLUSH );

At this time, we need to look at the scroroller class. It is similar to a computing center. It usually uses the scroroller object to record or calculate the scroll position of the View. When we click the Button, scroroller calls the startScroll method, the four parameters in this method are the benchmark position and offset, which only records these parameters,

Public void startScroll (int scrollX, int scrollY, int distanceX, int distanceY ){

StartX = scrollX;

StartY = scrollY;

This. distanceX = distanceX;

This. distanceY = distanceY;

IsFinish = false;

StartTime = SystemClock. uptimeMillis ();

}

Then, send the handler information. In the handler, first judge scroler. computescroloffset (). If this returns true, it indicates that it is still moving,

/**

* Calculate the offset. This method is used to determine the position of the current execution.

* @ Return true still moving false: the movement is stopped, and the duration is 1000l.

*/

Public boolean computescroloffset (){

 

If (isFinish ){

Return false;

}

 

Long timePassed = SystemClock. uptimeMillis ()-startTime;

 

If (timePassed <duration ){

 

CurrentX = (int) (startX + distanceX * timePassed/duration );

CurrentY = (int) (startY + distanceY * timePassed/duration );

 

System. out. println ("currentX:" + currentX );

} Else if (timePassed> = duration ){

CurrentX = startX + distanceX;

CurrentY = startY + distanceY;

IsFinish = true;

}

Return true;

}

  

Handler defined:

Private Handler handler = new Handler (){

 

@ Override

Public void handleMessage (Message msg ){

If (msg. what = FLUSH ){

If (scroller. computescroloffset ()){

Layout2.scrollTo (scroller. getCurrX (), 0 );

Handler. sendEmptyMessage (FLUSH );

}

}

}

};

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.