Swipe as one of the most basic effects in Android, using a wide range of scenarios. There are many ways to achieve this, and understand the various ways to implement sliding. Clearly in the development according to their actual needs, choose a reasonable implementation plan. This article from: ScrollTo ()/scrollby () content Sliding | Animation Mode Slide | Modify layout parameters in three ways to do a brief analysis.
One, Scrollerto () &&scrollby () content sliding
Both of these methods are the sliding methods of the view, which means that each control can be slid by invoking both methods. The implementation nature of the Scrollby () method is also called the Scrollto () method, except that. The Scrollyto () method parameter determines the end point of the sliding position, that is, the absolute coordinates where to slide. The Scrollby () method parameter describes the distance to slide, how much to slide, according to the relative coordinates of the current position. The following through the source to explore:
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(); } }}public void scrollBy(int x, int y) { scrollTo(mScrollX + x, mScrollY + y);}
From the source code can be learned that the Scrollto () method will first determine whether the location of the starting and ending point is not the same, in line with the conditions of sliding, save and update the starting point coordinates, it is not difficult to understand the sliding end point becomes a new starting point. This causes the slide to be completed, then called the secondary method, if the parameter end coordinates and the starting point coordinates are the same, the slide will not take effect. The Scrollby () method calls the Scrollto method directly, adds the starting point coordinate position and the sliding distance, determines the end position, so that the end coordinate will never repeat with the starting point coordinates, the sliding has been effective. Give me a chestnut:
ScrollTo () Effect
btn_scroll.scrollTo(100,0)
Why did the button not move, how did the word move? Yes, the ScrollTo () method is to slide the contents of the view, call the button's ScrollTo () method to slide His word, TextView is the same, of course, button is also based on the nature of TextView. When customizing the view, the slide is the inside circle, the rectangle, the view ... When the button runs 100 pixels and then clicks Execute Scrollto (100,0), nothing happens, which is the absolute coordinate of the relative starting point.
Scrollby () Effect:
btn_scroll.scrollBy(100,0);
Always executes Scrollby (100,0) is in effect, which is relative coordinates relative to the current position. Conclusion: The two methods of ScrollTo () and Scrollby () are sliding the contents of the view without changing the view's true layout position.
Second, the animation way to achieve sliding
There are two main animation motion tween animation and property animation, both of which do not actually change the view layout location, it should be noted that the completion of the motion tween animation immediately back to the original position, is to manipulate the image, the Fillafter property to True will not have this effect, Property animations do not have this feature. Give me a chestnut:
/*补间动画*/TranslateAnimation animation = new TranslateAnimation(0,300,0,0);animation.setDuration(2000);animation.setFillAfter(true);btn_scroll.startAnimation(animation);/*属性动画*/ObjectAnimator animator = ObjectAnimator.ofFloat(btn_scroll,"translationX",0,300);animator.setDuration(2000).start();
Implementation results:
There is also a problem with tweened animation, when the button swipe is complete, then click on the button without any response, and click the position before the button can still trigger the onclick () event, property animation does not have this feature.
Third, modify layout parameters
The two ways mentioned above do not really change the layout parameters, this time finally said to change the layout parameters to achieve. This approach is to directly change the layout of the view in the parent view, for example, to implement the button to the right of 300 pixels, the left side of the view to the left side of the parent view Left,view right to the left side of the parent view to add 300px ( The view coordinate system is not very clear can refer to this article: coordinate system), to achieve, for example, a chestnut:
btn_scroll.layout(btn_scroll.getLeft()+300,btn_scroll.getTop(),btn_scroll.getRight()+300,btn_scroll.getBottom());
The effect and animation implementations are roughly the same, except that the animation can specify how long to complete the slide, while modifying the layout parameters and the Scrollto ()/scrollby () method is done in an instant! There are many ways to modify layout parameters, but the principle is the same.
Various ways to compare
Here is a direct excerpt from the "Android Development art exploration," The big guy summed up the comparison in place.
Look at Scrollto/scrollby this way. It is a native method that is provided by the view, which is designed to be used specifically for view sliding, it can be relatively simple to implement the sliding effect, and does not affect the inner element of the click event. But its shortcomings are obvious, it can only slide the contents of the view, You cannot swipe the view itself.
See the animation, through the animation to achieve the view of the sliding, this is the case. If it is Android3.0 and using attribute animation, then there is no obvious disadvantage in using this > method, if you use the View animation or use the property animation below Android3.0, Cannot change the properties of the view itself. In practice, if the animation element does not need to respond to the user's interaction, it is more appropriate to use the animation to do the sliding, otherwise it is not suitable. But the animation has a clear advantage, that is, some complex effects must be animated to achieve.
Finally look at the way to change the layout, it is in addition to the use of trouble points, there is no obvious disadvantage, its main use is some interactive view, because these view needs to interact with the user, There is a problem with animating directly. So this time we need to use the method of directly changing layout parameters to implement.
Scrollto/scrollby: Easy to use, suitable for sliding the view content.
Animations: Simple to use, primarily for view without interaction and for complex animation effects.
Change layout parameters: The operation is slightly more complex and is suitable for interactive view.
The implementation of the view swipe in Android you should know.