javaanimationcontrolssmoothingcpu-cycles

Java controls animation - how to make them smooth and efficient


I am programming some custom controls in Java and using animation for transitions/fades/movements.

The way I am doing this is that I am starting a new thread and making adjustments to variables and things are adjusted using paint() method.

Example:

Now, my question is, let's say for instance that I was implementing a fade in. What I would do is increment the alpha variable by byte x //between 0-255 and paint a rectangle where alphaLevel += x, for instance (in pseudo-code):

byte increment = 40;

for (byte i = 0; i < 255; i += increment)
{
    _parentClass.setAlphaLevel (i);
    _parentClass.repaint();
    Thread.sleep (10);    
}

_parentClass.setAlphaLevel (255);

What I want to know is what is the lowest and what is the highest I should set the sleep to so that the animation doesn't look choppy? Does it have anything todo with the target device refresh rates or anything todo with the human eyes? Same question again with step. Is there a website that will give me good figures I can copy.

The reason I ask, is to maximize efficiency as it is going to be run on a battery operated device so more CPU time = less battery. What would you set it to?

Thanks.


Solution

  • The technique you're looking for is called "double buffering."

    I regretfully don't have time to show you an example, but that is what you need to look into.