2014-02-22 61 views
1

我想对我的图像做一个多动画(出现 - >旋转 - >消失)。我有这样的代码:动画出现,旋转和消失

fade_in.xml

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

<alpha 
    android:duration="1" 
    android:fromAlpha="0" 
    android:toAlpha="100" /> 

</set> 

fade_out.xml

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

<alpha 
    android:duration="1" 
    android:fromAlpha="100" 
    android:toAlpha="0" /> 

</set> 

image_rotate.xml

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

<rotate 
    android:duration="2500" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:toDegrees="120" /> 

</set> 
在我的Java代码

另外:

animRotate= AnimationUtils.loadAnimation(context, R.anim.image_rotate); 
animRotate.setDuration((long) duration); 
fade_in = AnimationUtils.loadAnimation(context, R.anim.fade_in); 
fade_out = AnimationUtils.loadAnimation(context, R.anim.fade_out); 

AnimationSet s = new AnimationSet(false); 
s.addAnimation(fade_in); 
s.addAnimation(animRotate); 
s.addAnimation(fade_out); 

image.startAnimation(s); 

但不幸的是它不能正常工作...

+0

不correcrly工作? – pskink

回答

3

你有你的动画XML文件severals错误:

  • 时间属性是毫秒,因此1ms的是太短了显着淡入/淡出动画
  • alpha属性是0和1之间的浮点数,100的方式太多了。
  • 你不需要在XML文件中的一组,如果只有一个动画:只需添加字母或旋转标签作为根

所以,你现在应该有这些文件:

fade_in.xml

<?xml version="1.0" encoding="utf-8"?> 
<alpha xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="1000" 
    android:fromAlpha="0" 
    android:toAlpha="1" /> 

fade_out.xml

<?xml version="1.0" encoding="utf-8"?> 
<alpha xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="1000" 
    android:fromAlpha="1" 
    android:toAlpha="0" /> 

image_rotate.xm l

<?xml version="1.0" encoding="utf-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="2500" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:toDegrees="120" /> 

然后,在您的代码中,您需要在每个动画之间添加一个偏移量。否则,所有动画将同时触发。此外,fillAfter标志必须根动画对象上设置(在这里,你AnimationSet

Animation animRotate= AnimationUtils.loadAnimation(context, R.anim.image_rotate); 
Animation fade_in = AnimationUtils.loadAnimation(context, R.anim.fade_in); 
Animation fade_out = AnimationUtils.loadAnimation(context, R.anim.fade_out); 

AnimationSet s = new AnimationSet(false); 
s.addAnimation(fade_in); 

animRotate.setDuration((long) duration); 
animRotate.setStartOffset(fade_in.getDuration()); 
s.addAnimation(animRotate); 

fade_out.setStartOffset(fade_in.getDuration() + animRotate.getDuration()); 
s.addAnimation(fade_out); 

s.setFillAfter(true); 

image.startAnimation(s);