The View of sliding

There are three ways to implement it

  1. The sliding is done through the scrollTo/scrollBy methods that the animation provides to the View itself
  2. Slide by animating the View with a pan effect
  3. Slide by changing the View’s LayoutParams to rearrange the View

Through the scrollTo/scrollBy

  1. ScrollTo implements absolute sliding based on passed parameters

  2. ScrollBy implements relative sliding of the base’s current position

  3. In the source code, scrollBy actually calls scrlloTy as well

public void scrollTo(int x, int y){~ ~ ~ ~ ~}public void scrollBy(int x, int y){
    scrlloTo(mScrollX + x, mScrollY + y);
}
Copy the code

ScrollTo and scrollBy can only change the position of the contents of the View, not the position of the View in the layout.


Sliding process
  1. MScrollX is always equal to the horizontal distance between the left edge of the View and the left edge of the View content

  2. MScrollY is always the vertical distance between the top edge of the View and the top edge of the View content

  3. The View edge is the position of the View and consists of four vertices. The View content edge is the content edge of the value View

  4. MScorllX and mScorllY are in pixels

  5. MScrollX is positive when the left edge of the View is to the right of the left edge of the View content, and negative when the left edge of the View is to the right (negative if you swipe left to right, and positive if you swipe left to right)

  6. MScrollY is positive when the upper edge of the View is below the upper edge of the View content, and negative when the upper edge of the View content is below the upper edge of the View content.

Use animation

methods

  • Use the traditional View animation
<? = XML version = "1.0" encoding = "utf-8"? > <set xmlns:android="http://schemas.android.com/apk/res/android" anroid:fillAfter="true" android:zAdjustment="normal" <translate android:duration="100" android:fromXDelta="0" android:fromYDelta="0" Android :interpolator="@android:anim/linear_interpolator" Android :toXDelta="100" Android :toYDelta="100"/> </set> at 100ms Move a View 100 pixels from its original position to the lower right cornerCopy the code
  • Using attribute animations (not available on Android3.0, need to use the open source animation library nineoldanroids)
ObjectAnimator.ofFloat(targetView, "translationX".0.100).setDuration(100).start();
Copy the code

disadvantages

View animation is an operation on the image of the View, which cannot really change the position parameters of the View, including width/height. If you want to preserve the state after animation, you must set the property of fillAfter to true, otherwise the animation effect will disappear after the animation is completed

A problem with a View animation is that it can’t change the position of the View. For example, if you animate a Button and move it 100px to the right, and the View is set to click, you will find that clicking on the new position does not trigger onClick. Clicking on the original location can still trigger the onClick event

Android 3.0 can use property animation to solve this problem

Changing layout Parameters

Changing layout parameters changes LayoutParams

For example, to shift a button 100px to the right, increase the marginLeft parameter in the Button’s LayoutParams by 100px

Or place an empty View to the left of the Button with a default width of 0. When we need to move the Button to the right, we simply reset the width of the empty View. When the width of the View increases, the Button is automatically squeezed to the right to achieve a panning effect.

MarginLayoutParams params = (MarginLayoutParams)mButton1.getLayoutParams();
params.width += 100;
params.leftMargin +=100;
mButton1.requestLayout();
/ / or mButton1. SetLayoutParams (params)
Copy the code

Comparison of various sliding modes

scrollTo/scrollBy animation Change the layout
Applicable scenario Simple operation, suitable for sliding View content Simple operation, mainly suitable for no interactive View content sliding
Features and Advantages and disadvantages The system provides the native method, specifically for the View of the slide, can slide the content of the View, can not slide the View itself Android 3.0 has no obvious disadvantages, but below 3.0 neither View animation nor property animation can change the properties of the View itself. In practical use, if animation elements do not need to respond to user interaction, then animation is suitable for sliding