动画显示,旋转和消失

时间:2014-02-22 12:44:52

标签: java android xml animation rotation

我想在我的图片上做多个动画(显示 - >旋转 - >消失)。我有这个代码:

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);

但不幸的是它无法正常工作......

1 个答案:

答案 0 :(得分:3)

动画xml文件中有多处错误:

  • 持续时间属性以毫秒为单位,因此1ms对于明显的淡入/淡出动画而言太短了
  • alpha属性是介于0 et 1,100之间的浮点数太多了。
  • 如果只有一个动画,则不需要在xml文件中设置:只需将alpha或rotate标记添加为根

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

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.xml

<?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);
相关问题