ImageView
我正在尝试使用动画淡出。动画运行,淡出ImageView
,但随后图像恢复到完全不透明。为什么会这样?
private void fadeOutImage() {
final ImageView imageView = (ImageView) activity.findViewById(R.id.imageView);
imageView.startAnimation(AnimationUtils.loadAnimation(activity, R.anim.fadeout));
}
我尝试将 alpha 设置为零,如下所示,但图像从未出现过。为什么不?
private void fadeOutImage() {
final ImageView imageView = (ImageView) activity.findViewById(R.id.imageView);
imageView.setAlpha(0);
imageView.startAnimation(AnimationUtils.loadAnimation(activity, R.anim.fadeout));
}
为了让它工作,我必须创建一个AnimationListener
,重载onAnimationEnd
,并将 alpha 设置为零。为什么?
final ImageView imageView = (ImageView) activity.findViewById(id);
Animation animation = AnimationUtils.loadAnimation(activity, R.anim.fadeout);
animation.setAnimationListener(new AnimationListener() {
@Override public void onAnimationStart(Animation animation) {
imageView.setAlpha(255);
}
@Override public void onAnimationRepeat(Animation animation) { }
@Override public void onAnimationEnd(Animation animation) {
imageView.setAlpha(0);
}
});
imageView.startAnimation(animation);
动画 XML:
<alpha
android:duration="1500"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0.0" />