我需要创建动画,在其中显示 3 个图像。每个图像应完全淡入和淡出(缓慢闪烁),然后应显示下一个图像。我通过创建 3 个动画来完成。每个动画在 onAnimationEnd 监听器中开始下一个动画:
private void startAnimation() {
final Animation aniIn = AnimationUtils.loadAnimation(this,
R.anim.fade_in_out);
final Animation aniIn1 = AnimationUtils.loadAnimation(this,
R.anim.fade_in_out);
final Animation aniIn2 = AnimationUtils.loadAnimation(this,
R.anim.fade_in_out);
aniIn.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
ivRandom0.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
ivRandom0.setVisibility(View.INVISIBLE);
ivRandom1.startAnimation(aniIn1);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
aniIn1.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
ivRandom1.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
ivRandom1.setVisibility(View.INVISIBLE);
ivRandom2.startAnimation(aniIn2);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
aniIn2.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
ivRandom2.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
showFinal();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
ivRandom0.startAnimation(aniIn);
}
对于该动画,我有一个 xml,它将 alhpa 从 0 设置为 1,然后又设置回 0:
fade_in_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:duration="300"
android:fromAlpha="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toAlpha="1"
android:repeatCount="1"
android:repeatMode="reverse"
/>
</set>
这是我的布局:
<RelativeLayout
android:id="@+id/images_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/iv_image_random_0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/img_generator0"
android:visibility="visible" />
<ImageView
android:id="@+id/iv_image_random_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/img_generator1"
android:visibility="invisible" />
<ImageView
android:id="@+id/iv_image_random_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/img_generator2"
android:visibility="invisible" />
</RelativeLayout>
虽然这有效,但我不喜欢嵌套回调。它还会导致内存泄漏,因为它在回调中保存对 ImageView 的引用。
有没有更好的方法来创建这种动画?或者至少避免内存泄漏?我正在尝试图像切换器,但它导致图像交叉淡入淡出,这是我不想要的。
谢谢!
最佳答案
你可以使用ObjectAnimator,它有设置Duration和开始延迟的方法,你可以根据你的需要计算。
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "alpha",0, 1);
objectAnimator.setDuration(DEFAULT_ANIMATION_DURATION);
objectAnimator.setStartDelay(milliseconds);
objectAnimator.start();
您可以创建三个不同的对象动画师来设置相应的。了解更多refer this link
关于android - 使用淡入淡出动画更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43365281/