2016-12-06 58 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。