Exploring slide screens in Android -- scrollto and scrollby Methods

Source: Internet
Author: User
Tags call back

 

This article is original and must be reproduced with the following source:Http://blog.csdn.net/qinjuning

 

Today, I will introduce you to the basic implementation process and principle of the sliding screen function in Android. I will give you a detailed explanation of the scrollto and

The difference between the two functions of scrollby.

First, we must understand thatThe android view has no boundaries and the canvas has no boundaries.But when we draw a specific view

The canvas object has some operations, such as translate (translation) and cliprect (CUT), to meet our requirements for drawing the canvas object,

We can call this borderless View"View coordinates"----- It is not restricted by the physical screen. Normally, a layout file is

When the display area of the graph exceeds this display area, it cannot be displayed in the area of the parent view. Correspondingly, we can refer to this boundary view as"Layout coordinates"

------ Layout size allocated by the parent View to the Child view.In addition, the starting coordinate of a view on the screen is located at the starting point of the view coordinate., As shown in.

 

In this case, the world is boundless, but our eyes and our hearts constrain the "world" we see ".

 

As follows:

The black box indicates the layout coordinates of the Child view, and the brown box indicates the View coordinates of the Child view-the coordinates are infinite, exceeding the limit of the parent View to the Child view.

After the specified region, the content exceeding the limit is no longer displayed.

The following question is: how can we display arbitrary coordinates of a view to the central coordinates of the view? Because the layout can only display specific

Therefore, we need to use scrollto () or scrollby () to "scroll" The expected View to the layout coordinate.

 

The following two variables and corresponding attribute methods are provided in view. Java to read the scroll value:

/*** The offset, in pixels, by which the content of this view is scrolled * horizontally. * {@ hide} */protected int mscrollx; // the content of this view is equivalent to the offset of the starting coordinate of the view, X axis direction/*** the offset, in pixels, by which the content of this view is scrolled * vertically. * {@ hide} */protected int mscrolly; // the content of this view is equivalent to the offset of the starting coordinate of the view, and the Y axis direction/*** return the scrolled left position of this view. this is the left edge of * The displayed part of your view. you do not need to draw any pixels * farther left, since those are outside of the frame of your view on * screen. ** @ return the left edge of the displayed part of your view, in pixels. */public final int getscrollx () {return mscrollx;}/*** return the scrolled top position of this view. this is the top edge of * The displayed part of your view. you do not need to draw any pixels abve * it, since those are outside of the frame of your view on screen. ** @ return the top edge of the displayed part of your view, in pixels. */public final int getscrolly () {return mscrolly ;}

 

Note that the so-called "by which the content of this view is scrolled" indicates that the offset is only applicable to

The specific content is implemented, rather than the background image. For specific reasons, see <Analysis of view rendering process and invalidate () in Android>

 

Tip: the current view content mentioned below is drawn at the layout coordinate.

 

Public voidScrollto(Int x, int y)

Note: The content of the current view is offset to (x, y) coordinates, that is, the display (visible) area is located at (x, y) coordinates.

Method prototype: view. Java class

/*** Set the scrolled position of your view. this will cause a call to * {@ link # onscrollchanged (INT, Int, Int, INT)} and the view will be * invalidated. * @ Param x the X position to scroll to * @ Param y the Y position to scroll to */Public void scrollto (int x, int y) {// If (mscrollx! = X | mscrolly! = Y) {int oldx = mscrollx; int Oldy = mscrolly; mscrollx = x; // assign a new value to save the current cheap quantity of mscrolly = y; // call back the onscrollchanged method onscrollchanged (mscrollx, mscrolly, oldx, Oldy); If (! Awakenscrollbars () {invalidate (); // usually causes re-painting }}}

 

Public voidScrollby(Int x, int y)

Note: The offset (x, y) Units continue in the current view, and the (visible) area is also offset (x, y) units.

Method prototype: view. Java class

/*** Move the scrolled position of your view. this will cause a call to * {@ link # onscrollchanged (INT, Int, 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 * // check the cause .. Mscrollx and mscrolly represent the position of our current offset, and continue the offset (x, y) units in the current position public void scrollby (int x, int y) {scrollto (mscrollx + X, mscrolly + Y );}

The first demo is very simple. You can understand and understand the usage and differences between the scrollto () and scrollby () functions.

 

The second demo is like a launcher, which allows you to switch between the left and right screens. The following functions are implemented: a custom viewgroup is used.

The object contains three linearlayout subviews, which are displayed on the viewgroup with certain layout coordinates (specified by the layout () method. Next, you can call

The scrollto or scrollby () method of the viewgroup object switches to the specified view content, that is, the screen is switched. It's fun.

 

If you do not understand the view rendering process, refer to my blog<Analysis of view rendering process and invalidate () in Android>.

As follows:

The custom viewgroup is as follows:

// Custom viewgroup, which contains three linearlayout controls, which are stored in different layout locations. Use scrollby or scrollto to switch between public class multiviewgroup extends viewgroup {private context mcontext; private Static string tag = "multiviewgroup"; Public multiviewgroup (context) {super (context); mcontext = context; Init () ;}public multiviewgroup (context, attributeset attrs) {super (context, attrs); mcontext = context; Init ();} private void Init () {// initialize three linearlayout controls linearlayout onell = new linearlayout (mcontext ); onell. setbackgroundcolor (color. red); addview (onell); linearlayout twoll = new linearlayout (mcontext); twoll. setbackgroundcolor (color. yellow); addview (twoll); linearlayout threell = new linearlayout (mcontext); threell. setbackgroundcolor (color. blue); addview (threell);} // measure process @ overrideprotected void onmeasure (INT widthmeasurespec, int heightmeasurespec) {log. I (TAG, "--- start onmeasure --"); // sets the size of the viewgroup int width = measurespec. getsize (widthmeasurespec); int Height = measurespec. getsize (heightmeasurespec); setmeasureddimension (width, height); int childcount = getchildcount (); log. I (TAG, "--- onmeasure childcount is -->" + childcount); For (INT I = 0; I <childcount; I ++) {view child = getchildat (I ); // set the size of each sub-view, that is, full screen child. measure (multiscreenactivity. screenwidth, multiscreenactivity. scrrenheight) ;}}// layout process @ overrideprotected void onlayout (Boolean changed, int L, int T, int R, int B) {// todo auto-generated method stublog. I (TAG, "--- start onlayout --"); int startleft = 0; // The start layout coordinate int starttop = 10 for each subview; // The spacing is set to 10px, which is equivalent to Android: margintop = "10px" int childcount = getchildcount (); log. I (TAG, "--- onlayout childcount is -->" + childcount); For (INT I = 0; I <childcount; I ++) {view child = getchildat (I ); child. layout (startleft, starttop, startleft + multiscreenactivity. screenwidth, starttop + multiscreenactivity. scrrenheight); startleft = startleft + multiscreenactivity. screenwidth; // calibrate the initial layout position of each sub-view. // The distribution of the three sub-Views on the screen is as follows: [0,320]/[320,640]/[640,960]}

 

PS: You can add several sub-views, such as textview and button, to these linearlayout respectively.

 

As for the implementation of the sliding screen function of launcher, I try to master it. Maybe it's just a dull day. It's a catch for the scoller class. I haven't mastered it yet, but here

I would like to recommend some good learning resources for you. If you need it in the future, take it with us. Bytes

 

1. scoller class introduction:Android Chinese API (64) -- scroller

2. Summary of related resources:Http://blog.csdn.net/dellheng/article/details/7164275 

3,Launcher modification-source code tracking for switching between left and right sliding screens

 

The sample source code is located: Http://download.csdn.net/detail/qinjuning/4054840

 

Supplement (remove the above paragraph,-): For how to implement the touch slide screen-imitation launcher slide screen and scoller class usage, see my

Another blog post: Slide screen implementation in Android ---- hands-on instructions on how to implement a touch slide screen and scroroller




 

 

 

 

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.