Android ImageView更改alpha动画

问题描述:

我有四个需要加载的图像。我想要一个动画播放,等待500毫秒,另一个播放,等待500毫秒,等等。所有的动画都是将alpha从255更改为0,然后返回到255.所有四个imageView都需要该动画。Android ImageView更改alpha动画

我目前有两个问题。

1.)所有图像同时播放。
2.)下次调用该方法时,动画不起作用。

public void computerLights() 
{ 

    ImageView green = (ImageView)findViewById(R.id.imgViewGreen); 
    ImageView red = (ImageView)findViewById(R.id.imgViewRed); 
    ImageView blue = (ImageView)findViewById(R.id.imgViewBlue); 
    ImageView yellow = (ImageView)findViewById(R.id.imgViewYellow); 

    AlphaAnimation transparency = new AlphaAnimation(1, 0); 

    transparency.setDuration(500); 
    transparency.start(); 
    green.startAnimation(transparency); 
    red.startAnimation(transparency); 
    blue.startAnimation(transparency); 
    yellow.startAnimation(transparency); 
} 

我不知道这是否是最好的解决方法,但你可以用,你可以在500周毫秒的时间间隔将消息发送给处理器实现这一目标相当容易。

private int mLights = new ArrayList<ImageView>(); 
private int mCurrentLightIdx = 0; 
private Handler mAnimationHandler = new Handler(){ 

    @Override 
    public void handleMessage(Message msg) { 
     super.handleMessage(msg); 

     ImageView currentLightIdx = mLights.get(currentLight); 

     AlphaAnimation transparency = new AlphaAnimation(1, 0); 

     transparency.setDuration(500); 
     transparency.start(); 
     currentLight.startAnimation(transparency); 

     currentLightIdx++; 
     if(currentLightIdx < mLights.size()){ 
      this.sendMessageDelayed(new Message(), 500); 
    } 
}; 

public void computerLights() 
{ 

    ImageView green = (ImageView)findViewById(R.id.imgViewGreen); 
    ImageView red = (ImageView)findViewById(R.id.imgViewRed); 
    ImageView blue = (ImageView)findViewById(R.id.imgViewBlue); 
    ImageView yellow = (ImageView)findViewById(R.id.imgViewYellow); 

    mLights.add(green); 
    mLights.add(red); 
    mLights.add(blue); 
    mLights.add(yellow); 

    mAnimationHandler.sendMessage(new Message()); 
} 

发送第一条消息后,处理程序将继续每隔500ms发送一次消息,直到所有动画都已启动。