Search code examples
androidanimationtranslate-animation

%p in android animation


I'm confused about %p unit in animation. Here is my anim file test.xml

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:duration="1000"
    android:fillAfter="true"
    android:fromXDelta="0"
    android:toXDelta="0"
    android:fromYDelta="50%p"
    android:toYDelta="50%p" />

And I use this animation for my view

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:layout_alignParentTop="true"
        android:background="#ff2"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/view" />

</RelativeLayout>

And after animated, the view is nearly center the screen, as I expected. enter image description here

But, if I change the position of the view to center it's parent, and the position of the view after animation is not correct

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:layout_centerVertical="true"
        android:background="#ff2"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/view" />

</RelativeLayout>

Here is the result: enter image description here

I think for the same parent, 50%p is alway the same, but the result is not. Why?


Solution

  • you can try

    android:fromYDelta="0%p"
    android:toYDelta="50%p"
    

    then you will see your animated in 1000ms

    so

    • the android:fromYDelta= means the start position based on viewself
    • the android:toYDelta= means the end position based on viewself

    when you set the View android:layout_alignParentTop="true" ,the View animated from top to center.

    when you set the View android:layout_alignParentCenter="true" ,the View animated from center to bottom.