我得到了一些非常奇怪的行为与Android动画,我想了解是怎么回事。
ImageView
在xml中是不可见的,我没有设置
alpha
.
图片框
if (isFadeIn) {
myImage.animate().alpha(1.0f).setDuration(5000).withStartAction(() -> {
myImage.setVisibility(View.VISIBLE);
myImage.setAlpha(0.0f);
});
} else {
myImage.animate().alpha(0.0f).setDuration(5000)
.withEndAction(() -> myImage.setVisibility(View.INVISIBLE));
}
现在,此代码会淡入淡出…..但第一次运行时不会淡入淡出,淡出总是闪烁:
if (isFadeIn) {
myImage.animate().alpha(1.0f).setDuration(5000).withStartAction(() -> {
myImage.setVisibility(View.VISIBLE);
myImage.setAlpha(0.0f);
}).withEndAction(() -> myImage.setAlpha(1.0f));
} else {
myImage.animate().alpha(0.0f).setDuration(5000)
.withStartAction(() -> {
myImage.setAlpha(1.0f);
myImage.setVisibility(View.VISIBLE);
})
.withEndAction(() -> myImage.setVisibility(View.INVISIBLE));
}
我绞尽脑汁想弄明白为什么会出现这些行为。
现在,这个代码起作用了:
fadeInAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
myImage.setVisibility(View.VISIBLE);
}
@Override public void onAnimationEnd(Animation animation) { }
@Override public void onAnimationRepeat(Animation animation) { }
});
fadeOutAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { }
@Override public void onAnimationEnd(Animation animation) {
myImage.setVisibility(View.INVISIBLE);
}
@Override public void onAnimationRepeat(Animation animation) { }
});
fadeInAnimation.setDuration(5000);
fadeOutAnimation.setDuration(5000);
...
if (isFadeIn) {
myImage.startAnimation(fadeInAnimation);
} else {
myImage.startAnimation(fadeOutAnimation);
}
但它太冗长了,我不喜欢它,但现在我坚持使用它,我只是想了解其他代码有什么问题。