Java 动画显示、旋转和消失

Java 动画显示、旋转和消失,java,android,xml,animation,rotation,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:durat

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

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>
<?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" />

但不幸的是,它不能正常工作…

您的动画xml文件中有几个错误:

  • duration属性以毫秒为单位,因此1ms对于明显的淡入/淡出动画来说太短了
  • alpha属性是介于0和1100之间的浮点值,太多了
  • 如果只有一个动画,则不需要在xml文件中设置:只需添加alpha或rotate标记作为根
因此,您现在应该拥有以下文件:

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>
<?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" />

不正常吗?不适合我。很抱歉。更好地使用此库:-
<?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" />
<?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" />
<?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" />
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);