%android动画中的p

%android动画中的p,android,animation,translate-animation,Android,Animation,Translate Animation,我对动画中的%p单位感到困惑。 这是我的动画文件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

我对动画中的%p单位感到困惑。 这是我的动画文件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" />

我在视图中使用这个动画

<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>

在设置动画后,视图几乎位于屏幕中央,正如我所预料的那样。

但是,如果我将视图的位置更改为其父视图的中心,并且动画后视图的位置不正确

<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>

结果如下:

我认为对于同一个父母来说,50%的磷总是一样的,但结果却不一样。为什么?

你可以试试

android:fromYDelta="0%p"
android:toYDelta="50%p"
然后,您将在1000毫秒内看到动画

所以

  • android:fromYDelta=表示基于viewself的起始位置
  • android:toYDelta=
    表示基于viewself的结束位置
当您设置视图时,
android:layout\u alignParentTop=“true”
,视图从顶部到中心设置动画

当您设置视图时,
android:layout\u alignParentCenter=“true”

,视图从中心到底部设置动画。

请参阅本文:50%p是否表示:currentY+50%*parentHeight?