Android Scroller (1) -- View understanding and usage of scrollTo () calls, scrollerscrollto

Source: Internet
Author: User

Android Scroller (1) -- View understanding and usage of scrollTo () calls, scrollerscrollto
MainActivity is as follows:

Package cc. uu; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; import android. app. activity;/*** Demo Description: * understanding of scrollTo () and scrollBy () and the use of ** principles: * 1 in fact, the View has no boundaries, only part of the View is displayed on the screen. * 2 scrollTo () and scrollBy () the essence is The same, but The representation is slightly different. * The two variables that are closely related to The two methods, mScrollX and mScrollY, can be seen in * View source code: ** // The offset, in pixe Ls, by which the content of this view is scrolled horizontally. * protected int mScrollX; ** // The offset, in pixels, by which the content of this view is scrolled vertically. * protected int mScrollY; ** the document description shows that * mScrollX and mScrollY indicate the horizontal or vertical offset between the content of the View and the View. ** scrollTo (int x, int y) method: * move the content of a view to the specified position. at this time, the offset of mScrollX and mScrollY are equal to x and y respectively. * By default, both mScrollX and mScrollY are 0. ** scrollBy (in T x, int y) method: * move the View content based on the existing one. * The source code of this method is very simple and also reflects this point. * public void scrollBy (int x, int y) {* scrollTo (mScrollX + x, mScrollY + y ); *} * by default, both mScrollX and mScrollY are 0 **. Re-emphasize and Note: * scrollTo () and scrollBy () Move only the View content, however, the View background is not moved. * In this example, the background color is added to the View. ** continue with the above question: * What if a ViewGroup (such as XXXLayout) calls the scrollTo (By) () method? * Its Content (that is, all its sub-views) will be moved. This can be seen in the next example. * ** 3 coordinate description of scrollTo (int x, int y) and scrollBy (int x, int y) Methods * For example, we call scrollTo () for a TextView) * How can the content (such as the displayed text: Hello) in the TextView be moved? * Move 25 units down? No, the opposite is true. * Why? * Calling these two methods will cause the view to be repainted. * call the public void invalidate (int l, int t, int r, int B) method. * Here, the l, t, r, and B parameters represent the original coordinates of the View. * This method will eventually call: * tflat. set (l-scrollX, t-scrollY, r-scrollX, B-scrollY); * p. invalidateChild (this, tflat); * Where tflat is ViewParent, tflat is Rect, and this is the original View. * The two lines of code redraw the View in a Rect. * Please note the first line of code: * The original l and r both subtract scrollX * The original t and B both subtract scrollY * that is, if scrollX is positive, in this case, the width of the repainted View is reduced. On the contrary, the height of the repainted View is reduced if scrollY is positive. On the contrary, the width of the repainted View is reduced, textView calls scrollTo () in contrast to our understanding. ** the scrollBy (int x, int y) method is similar to the preceding method. * ** instructions for this example, please refer to the following code comments *** references: * 1 http://blog.csdn.net/wangjinyu501/article/details/32339379 * 2 http://blog.csdn.net/qinjuning/article/details/7247126 * Thank you very much *** note: * The scrollTo (By) () method is fast and stiff. * To optimize the slide process of scrollTo (By) (), the scroroller class can be used. * The first sentence of the class source code is This class encapsulates scrolling. * Indicates the purpose of this class: encapsulates the sliding process. * In the following example, we will learn how to use Scroller. **/public class MainActivity extends Activity {private TextView mTextView; private Button mLeftButton; private Button mRightButon; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init ();} private void init () {mTextView = (TextView) findViewById (R. id. textView); mLeftButton = (Button) findViewById (R. id. leftButton); mLeftButton. setOnClickListener (new ClickListenerImpl (); mRightButon = (Button) findViewById (R. id. rightButton); mRightButon. setOnClickListener (new ClickListenerImpl ();}/*** example: * 1. When you click leftButton each time, * 1.1 calls scrollBy () to make mTextView content (text) move 30*1.2 to the left based on the original offset and call scrollBy () to let mLeftButton content (text) based on the original offset, it is also moved to the left for 30x2. When you click rightButton each time, * 2.1 calls scrollTo () to offset the content (text) of the mTextView directly to the right for 30, regardless of the previous BASICS (that is, mScrollX and mScrollY) * 3 click the leftButton several times in a row and the mTextView content (that is, text) will be moved 30 to the left each time, * click rightButton again to view the mTextView content (text), which is directed to the right 30 position at a time, rather than moving it slowly. * This operation ** 1 demonstrates the difference between the two methods. * 2 intuitively shows the effect of the scrollTo () method, regardless of the previous offset. * 4 in this example, we can also see that when the two methods are called, the View background is not moved. the content is moved. */private class ClickListenerImpl implements OnClickListener {@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. leftButton: // move the mTextView content to the left. scrollBy (30, 0); // move the mLeftButton content to the left. scrollBy (20, 0); break; case R. id. rightButton: // move the content of mTextView to the right to the position of-30 mTextView. scrollTo (-30, 0); break; default: break ;}}}}



























Main. xml is 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"> <TextView android: id = "@ + id/textView" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "@ string/hello_world" android: layout_centerHorizontal = "true" android: layout_marginTop = "20dip" android: background = "@ android: color/darker_gray"/> <Button android: id = "@ + id/leftButton" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "TextView to the left of the screen" android: layout_centerHorizontal = "true" android: layout_marginTop = "150dip"/> <Button android: id = "@ + id/rightButton" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "TextView to the right of the screen" android: layout_centerHorizontal = "true" android: layout_marginTop = "250dip"/> </RelativeLayout>



On Android, there is a scrollview and the following scrollview. How can I make the above scrollview scroll synchronously by dragging the following one?

The scrollto method may run faster at the front end of the scrollview.
 
How does android use scrollview to scroll the input text?

Set the ScrollView layout when creating an Activity, and add your own TextView control to the Activity.

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.