动画出现,旋转和消失

我想在我的图像上做多个动画(出现->旋转->消失)。我有以下代码:

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

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

絮状

您的动画xml文件中出现多个错误:

  • duration属性以毫秒为单位,因此1ms对于显着的淡入/淡出动画来说太短了
  • alpha属性是介于0和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" />

然后,在代码中,您需要在每个动画之间添加一个偏移量。否则,将同时触发所有动画。此外,必须在根动画对象(此处为your 上设置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);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章