在视图中使用淡入淡出animation

我想要一个最初是不可见的View ,当我按下一个button时,它会变成可见的,在animation中淡入淡出。 我正在使用AlphaAnimation的衰落效果。 问题是,如果我使视图不可见,animation将无法看到。

非常感谢,

的Gratzi

AnimationListener提供一个AnimationListener ,并在animation开始时使视图可见。

http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html

假设您的res \ anim \文件夹中有一个名为your_fade_in_anim.xmlImageView和一个animation文件your_fade_in_anim.xml

 ImageView imageView = (ImageView) findViewById(R.id.imageView); Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.your_fade_in_anim); // Now Set your animation imageView.startAnimation(fadeInAnimation); 

你的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:interpolator="@android:anim/accelerate_interpolator" android:duration="[duration (in milliseconds)]" android:repeatCount="infinite" /> </set> 

用你的实际持续时间replace括号。

而不是无限重复计数和隐藏/查看您的视图,我build议只是不重复animation,并开始以alpha通道设置为最大。 那么你可以使用:

 <?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:interpolator="@android:anim/accelerate_interpolator" android:duration="[duration (in milliseconds)]" android:repeatCount="0" /> </set> 

你完成了。 不需要听众,隐藏或显示。 就像一样简单。