Android ImageView改变alpha动画

时间:2013-04-07 03:47:22

标签: android android-animation

我有四张需要加载的图片。我想要播放一个动画,等待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);
}

1 个答案:

答案 0 :(得分:0)

我不确定这是否是最优雅的解决方案,但您可以使用处理程序轻松实现此目的,您可以以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());
}

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