Android 使用淡入/淡出动画更改背景图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24939387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android change background image with fade in/out animation
提问by BekaKK
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.
我编写了可以每 5 秒随机更改背景图像的代码。现在我想使用淡入/淡出动画来更改背景图像,但我不知道如何使用此动画。
This is a my source:
这是我的来源:
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.
如果有人知道解决方案,请帮助我,谢谢。
回答by kimkevin
You need to call these codes.
您需要调用这些代码。
First, you have to make two xml files for fade in & out animation like this.
首先,您必须为这样的淡入淡出动画制作两个 xml 文件。
fade_in.xml
淡入淡出.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
淡出.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>
Second, you have to run animation of imageView like below and also you have to set AnimationListener to change fadeout when fadein finish.
其次,您必须像下面一样运行 imageView 的动画,并且您还必须设置 AnimationListener 以在淡入完成时更改淡出。
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) {
}
});
回答by akshay7692
For fade in animation , create new folder named 'anim' in your res folder and inside it, create 'fade_in.xml' with following code
对于淡入动画,在 res 文件夹中创建名为“anim”的新文件夹,并在其中使用以下代码创建“fade_in.xml”
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="0.0"
android:interpolator="@android:anim/decelerate_interpolator"
android:toAlpha="1.0" />
</set>
now to run this animation on your imageview, use following code in your activity
现在要在您的 imageview 上运行此动画,请在您的活动中使用以下代码
Animation anim = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_in);
imageView.setAnimation(anim);
anim.start();
for fadeout animation, just swap values of android:fromAlpha and android:toAlpha
对于淡出动画,只需交换 android:fromAlpha 和 android:toAlpha 的值
Hope this helps.
希望这可以帮助。
回答by Ziwei Zeng
You can use relativeLayout, and add one layer of background view which set both height and width match_parent. All other UI element should put on top of this view. In your code. Define fadeOut and fadeIn animation. Find that background view by id, then do this:
您可以使用 relativeLayout,并添加一层设置高度和宽度 match_parent 的背景视图。所有其他 UI 元素都应放在此视图之上。在你的代码中。定义fadeOut 和fadeIn 动画。通过 id 找到该背景视图,然后执行以下操作:
xxxBackground.startAnimation(fadeOut);
xxxBackground.setBackgroundResource(R.drawable.your_random_pic);
xxxBackground.startAnimation(fadeIn);
You can use some interpolator to tune the performance.
您可以使用一些插值器来调整性能。
回答by Cabezas
You need AnimationDrawable with animation.
您需要带有动画的 AnimationDrawable。
First step AnimationDrawable
第一步 AnimationDrawable
-Create a file /res/anim/anim_android.xml
- 创建一个文件/res/anim/anim_android.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false">
<item android:drawable="@drawable/android_1"
android:duration="100"/>
<item android:drawable="@drawable/android_2"
android:duration="100"/>
<item android:drawable="@drawable/android_3"
android:duration="100"/>
<item android:drawable="@drawable/android_4"
android:duration="100"/>
<item android:drawable="@drawable/android_5"
android:duration="100"/>
<item android:drawable="@drawable/android_6"
android:duration="100"/>
<item android:drawable="@drawable/android_7"
android:duration="100"/>
</animation-list>
-Add a ImageView with android:src="@anim/anim_android".
- 添加一个带有 android:src="@anim/anim_android" 的 ImageView。
<ImageView
android:id="@+id/myanimation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@anim/anim_android" />
Second step
第二步
-In your activity create AnimationDrawable and Animation
- 在您的活动中创建 AnimationDrawable 和 Animation
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
animationDrawable.setOneShot(true);
animationDrawable.start();
Animation animation = AnimationUtils.loadAnimation(YourActivity.this, android.R.anim.fade_in);
imageView.setAnimation(animation);
animation.start();
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
Animation fadeOut = AnimationUtils.loadAnimation(YourActivity.this, android.R.anim.fade_out);
imageView.startAnimation(fadeOut);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
you don't need Handler.
你不需要处理程序。
回答by Bugs Happen
Answer by kimkevinanimates a View(be it an ImageView, TextViewetc.) and not a background of a View. Which means if you want to just highlight any View, you'll have to add another Viewbehind it (lets call it backgroundView) and animate that backgroundView.
由回答kimkevin动画一个View(可以是一个ImageView,TextView等等),而不是一个的背景View。这意味着如果您只想突出显示 any View,则必须View在其后面添加另一个(我们称之为 backgroundView)并为该 backgroundView 设置动画。
Use the following code for animating background of a view
使用以下代码为视图的背景设置动画
val startColor = view.solidColor
val endColor = ContextCompat.getColor(context, R.color.your_color)
val colorAnim = ObjectAnimator.ofInt(view, "backgroundColor", startColor, endColor, startColor)
colorAnim.duration = 2000
colorAnim.setEvaluator(ArgbEvaluator())
colorAnim.start()

