Codename One - Form.registerAnimated()有时不会注册动画

时间:2017-02-07 13:19:24

标签: java animation codenameone

我有实现动画的类。我将此课程注册为我的表格动画。它有效,但是20次(或关闭)中有1次未注册animate()根本没有被调用。

在下面显示的示例中,我们看到RollAnim2.start()被调用,但RollAnim2.animate()未被调用。

public class RollAnimation implements Animation {

    public void start () {
                myForm.registerAnimated(this);
                Log.p("RollAnim "+this.id+" :  starting!\ntargets: "+
                + targetFrames.toString() +"\ncurrent: " +
                + sTempFrames ());
            }

    public void stop () {
            myForm.deregisterAnimated(this);
            Log.p("RollAnim "+this.id+" :  stopped!\ntargets: "+
            +targetFrames.toString() +
            +"\ncurrent: " + sTempFrames ());
        }
    @Override
    public boolean animate() {
           Log.p("RollAnim "+this.id+" :  Animate ()");
           //Some mechanics here
           if (blablabla) {
                 this.stop ();
                 return false;
           } else 
                 return true;
}

并记录:

[Timer-20] 0:1:14,119 - RollAnim 2 :  starting!
targets: [30, 60, 90, 50, 30, 40]
current: [90, 80, 70, 30, 80, 20]
[Timer-16] 0:1:14,119 - RollAnim 0 :  starting!
targets: [0, 60, 70, 30, 50, 40]
current: [60, 0, 60, 80, 20, 80]
[Timer-19] 0:1:14,119 - RollAnim 4 :  starting!
targets: [60, 60, 30, 50, 50, 0]
current: [80, 30, 0, 10, 20, 50]
[Timer-18] 0:1:14,119 - RollAnim 3 :  starting!
targets: [70, 50, 70, 0, 70, 40]
current: [80, 20, 80, 10, 40, 0]
[Timer-17] 0:1:14,120 - RollAnim 1 :  starting!
targets: [10, 20, 30, 10, 60, 10]
current: [50, 10, 40, 70, 90, 60]
[EDT] 0:1:14,120 - RollAnim 0 :  Animate ()
[EDT] 0:1:14,121 - RollAnim 4 :  Animate ()
[EDT] 0:1:14,121 - RollAnim 3 :  Animate ()
[EDT] 0:1:14,121 - RollAnim 1 :  Animate ()
[EDT] 0:1:14,139 - RollAnim 0 :  Animate ()
[EDT] 0:1:14,139 - RollAnim 4 :  Animate ()
[EDT] 0:1:14,139 - RollAnim 3 :  Animate ()
[EDT] 0:1:14,139 - RollAnim 1 :  Animate ()
[EDT] 0:1:14,154 - RollAnim 0 :  Animate ()
[EDT] 0:1:14,154 - RollAnim 4 :  Animate ()
*** 
[EDT] 0:1:14,725 - RollAnim 3 :  stopped!
targets: [70, 50, 70, 0, 70, 40]
current: [70, 50, 70, 0, 70, 40]
[EDT] 0:1:14,878 - RollAnim 4 :  stopped!
targets: [60, 60, 30, 50, 50, 0]
current: [60, 60, 30, 50, 50, 0]
[EDT] 0:1:15,28 - RollAnim 0 :  stopped!
targets: [0, 60, 70, 30, 50, 40]
current: [0, 60, 70, 30, 50, 40]
[EDT] 0:1:15,58 - RollAnim 1 :  stopped!
targets: [10, 20, 30, 10, 60, 10]
current: [10, 20, 30, 10, 60, 10]

我该怎么做才能解决这个问题?

更新

这不是 Timer& EDT 交互问题。由此类检查为调解员:

public class AnimationCrutch implements Animation {

    protected ArrayList<Animation> toRegister = new ArrayList<Animation>();
    protected Form master;
    public void registerAnimated (Animation animation) {
        toRegister.add(animation);
    }
    public AnimationCrutch (Form master) {
        this.master = master;
        this.master.registerAnimated(this);
    }
    @Override
    public boolean animate() {
        if (toRegister.size() == 0) return false;
        for (Animation anim : toRegister) {
            this.master.registerAnimated(anim);
        }
        toRegister.clear();
        return false;
    }

    @Override
    public void paint(Graphics g) {
        // TODO Auto-generated method stub
    }

}

2 个答案:

答案 0 :(得分:1)

我不得不放弃取消注册以使应用按预期工作。希望保持animate()返回false并不是非常昂贵,因为不需要动画任何东西。

答案 1 :(得分:0)

获得注册/注销共生关系可能很棘手。

start()被调用之前,似乎调用了stop()。我建议检查一下这个特例。