Android动画重复
我对Android中的动画有问题。在动画的结束和开始之间,屏幕上的整个图像会闪烁,而不会产生alpha效果。动画以alpha 0开始,也以alpha 0结束。我有以下代码:Android动画重复,android,animation,repeat,Android,Animation,Repeat,我对Android中的动画有问题。在动画的结束和开始之间,屏幕上的整个图像会闪烁,而不会产生alpha效果。动画以alpha 0开始,也以alpha 0结束。我有以下代码: <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" > <alpha androi
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<alpha
android:duration="4000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/linear_interpolator"
android:toAlpha="1.0" />
<scale
android:duration="12000"
android:fromXScale="0.5"
android:fromYScale="0.5"
android:pivotX="50%"
android:pivotY="10%"
android:toXScale="3"
android:toYScale="2"
android:interpolator="@android:anim/linear_interpolator"/>
<alpha
android:startOffset="10000"
android:duration="1500"
android:fromAlpha="1.0"
android:interpolator="@android:anim/linear_interpolator"
android:toAlpha="0" />
</set>
这是因为XML文件中的动画是同时运行的。既有一个将Alpha从0更改为1的动画,也有一个将Alpha从1更改为0的动画。我假设您希望按顺序运行它们,因此应该将它们拆分为单独的文件。一种解决方案可能是这样的:(未经测试) fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<alpha
android:duration="4000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/linear_interpolator"
android:toAlpha="1.0" />
</set>
我不确定您需要在哪里设置可见和不可见,但您可以为每个对象附加AnimationListener,并根据需要放置它们
希望这有帮助:)这项工作:
animace.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
imageView.setAlpha(1f);
}
@Override
public void onAnimationEnd(Animation animation) {
imageView.setAlpha(0f);
imageView.startAnimation(animace);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
您的标题是关于run()的,但是您提供的代码片段中没有提到它,请发布完整的代码。您是否尝试
imageView.setAlpha(0f)调用imageView.setVisibility(View.visibility)之前,在onAnimationStart
中选择code>代码>?不,这不起作用,当将alpha设置为imageview时,imageview保持透明,动画什么也不显示。我在集合10000的第二个alpha开始处,这个动画还可以。现在多亏了@Stanislav,我找到了解决方案。我不知道为什么,但将可见性设置为“不可见”或“可见”不起作用(Android Studio不显示任何错误),但当我将此更改为在animationEnd设置alpha 0f,在AnimationStart设置1f时,它现在起作用了。事实上,我错过了这一设置。对不起:)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<alpha
android:startOffset="10000"
android:duration="1500"
android:fromAlpha="1.0"
android:interpolator="@android:anim/linear_interpolator"
android:toAlpha="0" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<scale
android:duration="12000"
android:fromXScale="0.5"
android:fromYScale="0.5"
android:pivotX="50%"
android:pivotY="10%"
android:toXScale="3"
android:toYScale="2"
android:interpolator="@android:anim/linear_interpolator"/>
</set>
imageView.startAnimation(fadeInAnimation);
imageView.startAnimation(scaleAnimation);
fadeInAmination.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation) {
imageView.startAnimation(fadeOutAnimation);
}
@Override
public void onAnimationRepeat(Animation animation) { }
});
animace.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
imageView.setAlpha(1f);
}
@Override
public void onAnimationEnd(Animation animation) {
imageView.setAlpha(0f);
imageView.startAnimation(animace);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});