2011-10-06 88 views
1

我想使用ViewAnimator从一个视图转换到另一个视图(在我的测试应用程序中,视图是TextView)。下面列出了我的两个动画。我看到的行为都是动画,只要我触发animator就开始动画,而不是让InAnimation运行,并且一旦它完成了OutAnimation运行。我看到的东西看起来像一个风车 - 旋转出来的视图垂直于旋转的视图。我想看到旋转的视图从它的正常水平位置(0度)到垂直(90度);然后我想看到旋转的视图从垂直(-90度)到水平(0度)。ViewAnimator使用旋转动画在视图之间转换

@阿尼姆/ rotate_out.xml

<?xml version="1.0" encoding="UTF-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromDegrees="0" android:toDegrees="90" android:pivotX="50%" 
    android:pivotY="50%" android:repeatCount="0" android:duration="500" 
    android:interpolator="@android:anim/linear_interpolator"> 
</rotate> 

@阿尼姆/ rotate_in.xml

<?xml version="1.0" encoding="UTF-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromDegrees="-90" android:toDegrees="0" android:pivotX="50%" 
    android:pivotY="50%" android:repeatCount="0" android:duration="500" 
    android:interpolator="@android:anim/linear_interpolator"> 
</rotate> 

,并在主要活动的onCreate ...

va = (ViewAnimator) findViewById(R.id.ViewFlipper01); 
va.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_in)); 
va.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_out)); 

什么想法?

回答

3

也许? @

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <rotate xmlns:android="http://schemas.android.com/apk/res/android" 
     android:fromDegrees="0" android:toDegrees="90" android:pivotX="50%" 
     android:pivotY="50%" android:repeatCount="0" android:duration="500" 
     android:interpolator="@android:anim/linear_interpolator"> 
    </rotate> 
    <rotate xmlns:android="http://schemas.android.com/apk/res/android" 
     android:fromDegrees="0" android:toDegrees="90" android:pivotX="50%" 
     android:pivotY="50%" android:repeatCount="0" android:duration="500" 
     android:interpolator="@android:anim/linear_interpolator"> 
    </rotate> 
</set> 
+0

我为InAnimation和OutAnimation设置了什么,因为现在只有一个anim xml文件? –

2

ViewAnimator的源代码可以发现here动画/ rotate_outin.xml。根据showOnly()方法,故意将两个动画并行开始,因此当一个视图“移出”时,另一个视图已经“移入”。 所以要实现你的目标,你必须在动画中添加一些延迟,以便在动画完成时开始。你可以设置例如android:duration =“500”用于out-animation,android:startOffset =“500”用于in-animation。只要确保两者的值相同即可。