Some summary of Android View sliding

 

                                      Some summary of Android View sliding

  I have always had a little understanding of Android's Scroll sliding. After reading the blog of the great god and the official Android documentation, the following is a summary of several methods for AndroidView sliding:

 

(1) Both scrollTo(x,y) and scrollBy(x,y) can slide the View. What is the difference between them?

 

scrollTo(x,y): This method allows the View to scroll a certain distance relative to the initial position.

     For example: I want to move the view to the coordinate point (100, 100), then my offset is (0,, 0) - (100, 100) = (-100, -100), I will execute view.scrollTo (-100,-100), to achieve this effect.

                    

scrollBy(x,y) : This method makes the View scroll a certain distance relative to the current position.

    It can be seen from the source code that it actually calls scrollTo(mScrollX + x, mScrollY + y);
mScrollX + x and mScrollY + y, which means that the offset is occurring on the basis of the original offset. Our current position offset. According to the movement in the parent class VIEW, if it moves beyond the place, it will not be displayed.

 

Difference: The same is that they slide relative to the inside of the parent container. The difference is that if the two methods are called only once, they will both slide to the same position, but the difference will come out when these two methods are called multiple times.

       scrollTo() is to slide a certain distance relative to the initial position. When it is called for the second time, the position of the view will not change, that is to say, it will only slide once.

     scrollBy() is to slide a certain distance relative to the current position. After multiple calls, the current position will be continuously updated, so the scrollBy() method will make the VIew continue to slide.

 

 (2) The startScroll() method in Scroller

 

 public void startScroll (int startX, int startY, int dx, int dy,int d)

  Start scrolling with the provided starting point and the distance to be swiped. Scrolling will use the default value of 250ms for the duration.

     parameter

       startX: Offset value for horizontal scrolling, in pixels. A positive value indicates that the scroll will scroll to the left

  startY: Offset for vertical scrolling, in pixels. A positive value indicates that the scroll will scroll up

  dx: The distance to slide in the horizontal direction, greater than 0 will make the scroll scroll to the left

  dy: the vertical sliding distance, greater than 0 will make the scroll scroll up

        d: swipe duration

 

The following describes the meanings of several methods that are related to coordinates and are easily confused:
1 mScrollX: indicates the offset in the x-horizontal direction from the starting position of the view

mScrollY: represents the offset in the y vertical direction from the starting position of the view

Note: mScrollX and mScrollY do not refer to coordinates, but offsets.

 

2 The getWidth() method is the width of the control relative to the screen. When the width of the control is greater than the width of the screen, the value is the width of the screen

 

3 The value of the getLeft(), getRight(), getBottom(), getTop() methods is the relative position of its parent view, not the position of the control relative to the coordinates

 

(3) The process of Scroller developing ViewGroup with scrolling is as follows

 If you use Scroller, the flow is as follows:

  1. You can initialize the Scroller constructor as required in a custom layout.

 

  2. Rewrite the onInterceptTouchEvent(MotionEvent ev) method to see if you want to intercept related events.

 

  3. Rewrite the onTouchEvent(MotionEvent event) method, and use computeScroll() and scrollTo and scrollBy methods according to the actions on the touch screen to slide the layout according to the finger.

 

  4.在触摸操作结束(MotionEvent.ACTION_UP)的时候,调用startScroll(int startX, int startY, int dx, int dy, int duration)方法,进行动画自动操作,来完成整个滚动流程。

 

 在此我推荐下面2篇关于利用Scroller开发带有滚动效果的ViewGroup的博客。认真搞懂其中一个,你的理解就更深了。

 http://blog.csdn.net/vipzjyno1/article/details/24664161

http://blog.csdn.net/guolin_blog/article/details/48719871

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326946678&siteId=291194637