Soheil
Soheil

Reputation: 1706

Why is my animation is not smooth when first playing?

I have made an animation by changing the value of the horizontal scroll bar of a JScrollPane, something like the transition between the screens of android OS. In the first seconds (up to 10 seconds) of running, if i ask for animation, it is not smooth and has jumping; but after a while that I play with that (ask for animation), it gets completely smooth. Why that happens and more important how to solve that?

I don't mean the first seconds of my animation is not smooth, I mean while I start my program the first times that I ask for animation that is not smooth but later, in the next times, that is smooth.

    final JScrollBar br = mainContainerScrollPane.getHorizontalScrollBar();
        ActionListener toTheRight = new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (br.getValue() + 140 <= destinationScreenValue)
            {
                br.setValue(br.getValue() + 140);
            } else
            {
                br.setValue(destinationScreenValue);
                currentScreenName = destinationScreenName;
                timer.stop();
            }
        }
    };
        timer = new Timer(10, toTheRight);
        timer.setRepeats(true);
        timer.setCoalesce(true);
        timer.start();

Upvotes: 0

Views: 441

Answers (1)

trashgod
trashgod

Reputation: 205785

Running at 100 Hz is going to saturate the EDT almost immediately, which will collide with normal JVM startup overhead. Some alternatives:

  • Use the approach shown here to check your animation budget.

  • Try profiling at a lower rate to find the optimal value for your target platform(s).

  • Specify an initial delay for the Timer that gets past the startup overhead.

  • Start with a lower frame rate and ramp up the value, using a shorter delay, after things settle down.

Addendum: Here' are some examples that dynamically vary the animation rate:

Upvotes: 2

Related Questions