2016-08-04 78 views
1

我对动画中的%p单位感到困惑。 这里是我的动画文件的test.xml%android png动画

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

和动画后,该视图大致中央屏幕上,如我所料。 enter image description here

但是,如果我改变看法的位置居中的父,和视图的位置后的动画是不正确的

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

下面是结果: enter image description here

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

+1

看到这篇文章:http://stackoverflow.com/a/4412183/5519005 –

+0

是50%的平均值:currentY + 50%* parentHeight? – maphongba008

回答

0

你可以尝试

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

然后你会看到你的动画在1000毫秒

所以

  • android:fromYDelta=意味着基于viewself
  • android:toYDelta=起始位置意味着结束基于viewself的位置

当你设置视图android:layout_alignParentTop="true" ,视图从顶部到中心的动画。

当您设置查看android:layout_alignParentTop="true" ,视图从中心到底部的动画。