Android ImageView改变alpha动画



我有四张图片需要加载。我想让一个动画播放,等待500毫秒,另一个播放,等待500毫秒,等等。动画所做的就是将alpha值从255变为0,然后再回到255。所有四个imageview都需要这个动画。

我现在有两个问题。

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);
}

我不确定这是否是最优雅的解决方案,但是您可以很容易地实现这一点,使用一个处理程序,您可以每500ms发送消息。

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发送消息,直到所有动画启动。

最新更新