简体   繁体   English

动画显示,旋转和消失

[英]Animations appear, rotate and disappear

I want to do a multiple animations on my image (appear -> rotate -> disappear). 我想在我的图像上做多个动画(出现 - >旋转 - >消失)。 I've got this code: 我有这个代码:

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

Also in my java code: 还在我的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);

but unfortunately it doesn't work correctly... 但不幸的是它无法正常工作......

you have severals errors in your animation xml files: 你的动画xml文件中有几个错误:

  • the duration property is in milliseconds, so 1ms is way too short for a noticeable fade in/fade out animation 持续时间属性以毫秒为单位,因此1ms对于明显的淡入/淡出动画来说太短了
  • the alpha property is a float between 0 et 1, 100 is way too much. alpha属性是0 et 1,100之间的浮点数太多了。
  • you don't need a set in your xml files if there is only one animation : just add the alpha or rotate tag as a root 如果只有一个动画,则不需要在xml文件中设置:只需将alpha或rotate标记添加为根

So, you should have now these files: 所以,你现在应该有这些文件:

fade_in.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 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 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" />

Then, in your code, you need a add an offset between each animation. 然后,在您的代码中,您需要在每个动画之间添加一个偏移量。 Otherwise, all the animations will be triggered at the same time. 否则,将同时触发所有动画。 Moreover, the fillAfter flag must be set on the root animation object (here, your AnimationSet ) 此外,必须在根动画对象上设置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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM