简体   繁体   English

Android LayoutAnimationController:如何强制第一个孩子在重复之前等待最后一个孩子的动画完成?

[英]Android LayoutAnimationController: How to force the first child to wait for the last child's animation to finish before repeating?

I'm trying to build an animation where this image: 我正在尝试在此图像上构建动画:

没有酒吧

Becomes this image: 成为此图像:

吧台

Where the three bars fade in like 1...2...3... then back to none. 三个条形像1 ... 2 ... 3 ...一样淡出,然后又回到无。 At this point the animation will repeat. 此时,动画将重复。 Here is what I tried to make work: 这是我尝试进行的工作:

    AlphaAnimation anim = (AlphaAnimation) AnimationUtils.loadAnimation(this, R.anim.fade_in);
    anim.setRepeatMode(Animation.RESTART);
    anim.setRepeatCount(Animation.INFINITE);
    anim.setStartOffset(3000);

    LayoutAnimationController controller = new LayoutAnimationController(anim, 0.5f);

    rl.setLayoutAnimation(controller);

    rl.startLayoutAnimation();

This almost works, but the problem is that the first child in the view group of those wifi bars doesn't wait for the last child to complete its animation before it goes off. 几乎可以用,但是问题在于这些wifi栏的视图组中的第一个孩子不会等待最后一个孩子在动画消失之前完成动画。 The result is a beat that looks like 1...2...3..1..2.3...1...3...1..2.3 instead of a nice 1..2..3..1..2..3. 结果是拍子看起来像1 ... 2 ... 3..1..2.3 ... 1 ... 3 ... 1..2.3,而不是漂亮的1..2..3。 .1..2..3。 I need to find a way to make the controller wait until the cycle is completed before setting the 1st child off again. 我需要找到一种方法让控制器等到周期完成后再关闭第一个孩子。 I haven't been able to find any solutions on this, does anyone have some input? 我还没有找到任何解决方案,有人有输入吗? I'm open to changing the way I'm doing this altogether, this LayoutAnimationController class just seemed to be the best way up until this point. 我愿意完全改变我的操作方式,到目前为止,这个LayoutAnimationController类似乎是最好的方法。

Thanks! 谢谢!

When I had to use an animation for a image, I prepared the frames. 当我必须为图像使用动画时,我准备了帧。 I used AnimatioDrawable with this class: 我在此类中使用了AnimatioDrawable:

* This class is a hack to simply get an on finish callback for an
* AnimationDrawable.
*
*/
public class CustomAnimationDrawable extends AnimationDrawable {
// We need to keep our own frame count because mCurFrame in AnimationDrawable is private
private int mCurFrameHack = -1;

// This is the Runnable that we will call when the animation finishes
private Runnable mCallback = null;

/*
* We override the run method in order to increment the frame count the same
* way the AnimationDrawable class does. Also, when we are on the last frame we
* schedule our callback to be called after the duration of the last frame.
*/
@Override
public void run() {
super.run();

mCurFrameHack += 1;
if (mCurFrameHack == (getNumberOfFrames() - 1) && mCallback != null) {
scheduleSelf(mCallback, SystemClock.uptimeMillis() + getDuration(mCurFrameHack));
}
}

/*
* We override this method simply to reset the frame count just as is done in
* AnimationDrawable.
*/
@Override
public void unscheduleSelf(Runnable what) {
// TODO Auto-generated method stub
super.unscheduleSelf(what);
mCurFrameHack = -1;
}

public void setOnFinishCallback(Runnable callback) {
mCallback = callback;
}

public Runnable getOnFinishCallback() {
return mCallback;
}

}

And in my code I use it like: 在我的代码中,我像这样使用它:

        CustomAnimationDrawable staticAnimation = new CustomAnimationDrawable();
    //add frames with resources and duration. step is just a class of mine
        staticAnimation.addFrame(getResources().getDrawable(step.getStepImageResId()), step.getStepDuration());
    staticAnimation.addFrame(getResources().getDrawable(step.getStepImageResId()), step.getStepDuration());
    staticAnimation.addFrame(getResources().getDrawable(step.getStepImageResId()), step.getStepDuration());
        staticAnimation.setOneShot(false);

        imgView.setBackgroundDrawable(staticAnimation);
staticAnimation.start();

Probably not the best solution out there, but it worked for my needs. 可能不是最好的解决方案,但是它可以满足我的需求。

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

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