Android使用淡入/淡出动画更改背景图像
我编写了可以每5秒随机更改背景图像的代码.现在我想使用淡入/淡出动画来更改背景图像,但是我不知道如何使用此动画.
I wrote code which can change background image random every 5 second.now i want to use fade in/out animation to change background image,but I do not know how I can use this animation.
这是我的消息来源:
void handlechange() {
Handler hand = new Handler();
hand.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// change image here
change();
}
private void change() {
// TODO Auto-generated method stub
Random rand = new Random();
int index = rand.nextInt(image_rundow.length);
mapimg.setBackgroundResource(image_rundow[index]);
handlechange();
}
}, 4000);
}
目前,一切正常.我可以随机更改背景图像,但是我不知道如何使用动画淡入/淡出.
At the moment everything is OK. I can change background image random,but I don't know how can I use animation fade in/out.
如果有人知道解决方案,请帮助我, 谢谢.
If anyone knows solution please help me, thanks.
您需要调用这些代码.
首先,您必须制作两个xml文件以用于淡入&这样的动画.
First, you have to make two xml files for fade in & out animation like this.
fade_in.xml
fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:fillAfter="true"
android:duration="2000"
/>
</set>
fade_out.xml
fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:fillAfter="true"
android:duration="2000"
/>
</set>
第二,您必须运行如下所示的imageView动画,并且还必须设置AnimationListener以在淡入完成时更改淡出.
Second, you have to run animation of imageView like below and also you have to set AnimationListener to change fadeout when fadein finish.
Animation fadeOut = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_out);
imageView.startAnimation(fadeOut);
fadeOut.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
Animation fadeIn = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_in);
imageView.startAnimation(fadeIn);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});