---
title: "Android使用Animation完成动画淡出效果避免重新恢复最后一帧亮度的办法"
author: "huyangwin"
date: "2012-08-02T13:51:52Z"
tags: []
categories: ["移动开发"]
slug: "android-animation"
draft: false
lastmod: "2012-08-02T13:53:03Z"
id: 1617009
url: "https://waynehu16.iteye.com/blog/1617009"
---

很多游戏中都有进场的动画，用移动位置，或者透明度变化，展示出一些诸如游戏logo之类的信息。在初步了解Android的Animation之后，我们可以做出这样的效果。

      首先在res文件夹下面新建用于动画显示效果的xml

 

 

```
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toAlpha="1.0" />

</set>
```

      之后，在需要展现动画的Activity类中声明出来

 

```
		logo = (ImageView) findViewById(R.id.logo);
		appAnim = AnimationUtils.loadAnimation(this, R.anim.welcome);
		appAnim.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getApplicationContext(),
						AnotherActivity.class);
				startActivity(i);
				finish();
			}
		});
		logo.startAnimation(appAnim);
```

 

      在理论上，我们也实现了这样一种效果，但是细心点，会发现我们的动画，从最暗变化到最亮，再从最亮返回到最暗，但是在跳转到另外一个Activity之前，那个设置了动画效果的ImageView又恢复了最亮，显然不符合我们的要求。

 

 

\----------------------------------------------------------------------------------------------------------------------------------

 

      解决办法：在那个声明了动画效果的xml中，在set标签中加上一条android:fillAfter="true" 表示动画结束时，停留在动画的最后一帧即可。

      注意此句不要放在&lt;alpha&gt;标签中了，否则没有作用。

 

```
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toAlpha="1.0" />

</set>
```