2016-08-04 71 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" ,視圖從中心到底部的動畫。