2016-12-06 22 views
0

我有一個垂直居中的視圖。我想要刪除layout_centerHorizontal屬性,以便它會上升到視圖的頂部,但我想爲該過渡製作動畫。動畫布局_中心水平的移除

XML:

<RelativeLayout 
    android:id="@+id/center_box" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="65sp" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    android:animateLayoutChanges="true" 
    > 
    ... 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:text="HELLO WORLD" 
     android:textColor="#fff" 
     android:maxLines="1" 
     android:gravity="center" 
     android:layout_centerVertical="true" 
     android:textSize="35sp" /> 
    ... 
</RelativeLayout> 

的Java

private void showSplits(){ 
    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) centerBox.getLayoutParams(); 
    lp.removeRule(RelativeLayout.CENTER_VERTICAL); 
    centerBox.setLayoutParams(lp); 
    //but I want to animate this instead... 
} 
+0

不要使用'sp'作爲保證金:) –

+0

哈哈,通常我不會。但是,我希望邊緣與固定在頂端的TextView排成一行,所以我決定做個例外。 –

回答

0

動畫代碼:

<?xml version="1.0" encoding="utf-8"?> 
<set 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:interpolator="@android:anim/linear_interpolator" 
android:fillAfter="true"> 

<translate 
android:fromYDelta="0%p" 
android:toYDelta="100%p" 
android:duration="800" /> 
</set> 

在Java函數代碼:

private void showSplits(){ 
centerBox.startAnimation(animFadein); 
} 

In Create View Code:

OnCreate{ 
// load the animation 
animMoveIn = AnimationUtils.loadAnimation(getApplicationContext(), 
R.anim.animate); 
animMoveIn.setAnimationListener(this); 
} 

而且你應該實現AnimationListener。