1

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" />
4

2 回答 2

7

当您启动动画时,它实际上会采用视图当前的样子,根据您提供的动画对其进行动画处理,但实际上不会更改视图的状态

This can get confusing, but in your first case, after the animation ends, the ImageView just goes back to whatever it looked like before the animation - in your case, the normal opaque.

In your second example, you are setting the alpha to zero before the animation starts, and so when the animation begins playing, the image is already invisible, and so it animates this invisible ImageView - which you can't see.

Therefore, the required sequence is to start the animation before changing the ImageView at all, let it play out, and then the moment the animation ends, change the state of the ImageView so that it disappears before it gets redrawn in it's original state. This is what overriding onAnimationEnd() does.

So, to recap, in the proper implementation, there are two things that affect how the ImageView gets drawn:

  • The animation, which affects how the ImageView looks only while the animation is playing.
  • The imageView.setAlpha(0); method call, to keep the image invisible once the animation finishes.

Note: to possibly increase performance, I'd suggest using

imageView.setVisibility(View.INVISIBLE); 

instead of imageView.setAlpha(0);

于 2012-08-29T05:22:45.833 回答
1

I'm trying to fade out an ImageView using an animation. The animation runs, fading out the ImageView, but then the image goes back to fully opaque. Why does this happen?

The animation's effects are temporary. Once the animation has completed, the view returns to its state before the animation began.

I tried setting the alpha to zero as below, but then the image never showed up period. Why not?

Though apparently undocumented, the fromAlpha and toAlpha parameters appear to act in a multiplicative fashion. 100% of zero is still zero. I verified this by starting the alpha to a very low value, and I could see that the animation caused the alpha to start at the low value and fade to transparent.

To get this to work, I had to create an AnimationListener, overload the onAnimationEnd, and set the alpha to be zero there. Why?

Since the animation's effects are temporary, you need to set the object to be transparent after the animation. You also have to make sure you reset the object to be opaque before you start the animation.

It looks like you can use an ObjectAnimator starting in API 11 to set the alpha (or any other property's) values directly using rather than multiplicatively.

于 2012-08-29T14:08:12.857 回答