安卓布局动画 Controller : How to force the first child to wait for the last child's animation to finish before repeating?

标签 android android-animation

我正在尝试构建一个动画,其中此图像:

No bars

变成这张图片:

Bars

三个条形像 1...2...3... 一样淡入,然后又回到无。此时动画将重复。这是我尝试做的工作:

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

几乎 可以工作,但问题是那些 wifi 栏的 View 组中的第一个子项没有等到最后一个子项完成其动画后才关闭。结果是一个看起来像 1...2...3..1..2.3...1...3...1..2.3 而不是漂亮的 1..2..3 的节拍。 .1..2..3。我需要找到一种方法让 Controller 等到循环完成后再将第一个 child 再次关闭。我还没有找到任何解决方案,有人有意见吗?我愿意改变我做这件事的方式,这个 LayoutAnimationController 类似乎是到目前为止最好的方式。

谢谢!

最佳答案

当我不得不为图像使用动画时,我准备了帧。我在这个类中使用了 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;
}

}

在我的代码中,我这样使用它:

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

可能不是最好的解决方案,但它可以满足我的需求。

关于安卓布局动画 Controller : How to force the first child to wait for the last child's animation to finish before repeating?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11249893/

相关文章:

java - 如何在动画时使 TranslateAnimation 更新位置

安卓 : Github library using in Android Studio

android - 应用程序进入后台时未触发 ionic 事件(平台暂停)

android - 如何使用复选框从电话中选择多个联系人

java - 初始应用程序介绍启动后启动启动屏幕

java - Android状态栏中的动画

android - 如何在 android 中使用 firebase 处理返回数据对象的 json

heap - 三星 Galaxy S3 的堆大小是多少?

android - 如何在 Android 中执行某些动画时播放声音

Android: Listview 弹跳到 scrollview