Android动画技术疑问:fromXDelta参数为何采用负值?
fromXDelta="-100%" in Android Translate Animations Great question! Let me break this down clearly for you, starting with the core coordinate system that Android uses for view animations.
Android Animation Coordinate Basics
In Android's view animation framework, the coordinate origin (0,0) sits at the top-left corner of the screen. Here's how the axes work:
- Moving right along the X-axis increases the value (positive direction)
- Moving left along the X-axis decreases the value (negative direction)
When you use percentage values for fromXDelta or toXDelta, that percentage refers to the width of the view being animated—not the screen width.
Breaking Down Your slide_in_left.xml Animation
Let’s look at your code again:
// slide_in_left.xml <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="-100%" android:toXDelta="0%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="700"/> </set>
fromXDelta="-100%": This sets the view’s starting position one full view-width to the left of its final on-screen position. Since the final position is0%(the view’s normal layout spot), the view starts completely hidden outside the left edge of the screen.toXDelta="0%": The view ends at its intended, visible position on the screen (no horizontal offset from its default layout location).
Put simply, this animation makes the view slide smoothly from outside the left edge of the screen into its normal spot, over a 700ms duration.
Contrasting with slide_in_right.xml
To reinforce the idea, let’s compare it to your working right-slide animation:
// slide_in_right.xml <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="100%" android:toXDelta="0%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="400"/> </set>
Here, fromXDelta="100%" places the view one full width to the right of its final position (hidden outside the screen’s right edge), so it slides in from the right instead of the left.
Key Takeaway
Negative percentage values for fromXDelta just mean the animation starts in the negative X direction (left) relative to the view’s final position, while positive values start in the positive X direction (right). It’s all about positioning the view off-screen before animating it into view.
内容的提问来源于stack exchange,提问作者abdullah celik

