简体   繁体   English

Android ImageView更改Alpha动画

[英]Android ImageView changing alpha Animation

I have four images that need loaded. 我有四个需要加载的图像。 I want one animation to play, wait 500ms, another one play, wait 500ms, etc.. All the animation does is change the alpha from 255 to 0 and then back to 255. All four imageViews need that animation. 我希望播放一个动画,等待500毫秒,播放另一个动画,等待500毫秒,等等。所有动画所做的就是将alpha值从255更改为0,然后又更改为255。所有四个imageViews都需要该动画。

I am having two problems currently. 我目前有两个问题。

1.) All of the images play at the same time. 1.)所有图像同时播放。
2.) The next time the method is called, the animations don't work. 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);
}

I'm not sure if this is the most elegant solution, but you could achieve this pretty easily with a handler that you can send messages to at 500ms intervals. 我不确定这是否是最优雅的解决方案,但是您可以使用处理程序轻松地实现此目的,该处理程序可以每隔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());
}

After the first message is sent the handler will continue to send messages every 500ms until all of the animations have been started. 发送第一条消息后,处理程序将继续每500毫秒发送一次消息,直到所有动画都已开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM