0
我想顯示淡入淡出動畫的圖像數量,它工作正常,但圖像在完成一次後不會重複。 我想在循環中顯示,我沒有得到什麼缺失任何人都有一個想法嗎?完成一次後圖像不重複
我的代碼看起來像下面
MainActivity.java
animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.anim_fade_in);
animationFadeOut = AnimationUtils.loadAnimation(this, R.anim.anim_fade_out);
Animation.AnimationListener animListener = new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
changeImage(animation);
}
};
// Set listener to animation
animationFadeIn.setAnimationListener(animListener);
animationFadeOut.setAnimationListener(animListener);
// Start fade-in animation
imgBanner.setImageResource(R.mipmap.banner1);
imgBanner.startAnimation(animationFadeIn);
public void changeImage(Animation animation) {
if (animation == animationFadeIn) {
// Start fade-out animation
imgBanner.startAnimation(animationFadeOut);
} else if (animation == animationFadeOut) {
count++;
// Set next image after fading out previous image
switch (count) {
case 1:
imgBanner.setImageResource(R.mipmap.banner2);
imgBanner.startAnimation(animationFadeIn);
break;
case 2:
imgBanner.setImageResource(R.mipmap.banner3);
imgBanner.startAnimation(animationFadeIn);
break;
case 3:
imgBanner.setImageResource(R.mipmap.banner4);
imgBanner.startAnimation(animationFadeIn);
break;
case 4:
imgBanner.setImageResource(R.mipmap.banner5);
imgBanner.startAnimation(animationFadeIn);
break;
default:
break;
}
}
}
anim_fade_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<alpha
android:duration="2000"
android:fromAlpha="0.1"
android:toAlpha="1.0" />
</set>
anim_fade_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
</set>