views:

98

answers:

2

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.

A: 

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.

Stargazer712
I don't believe that is what I am looking for. Thanks for your input anyways.
Ben
Well, it could be. You were talking about the optimal refresh rate to save battery while still reducing flickering. Double buffering will improve the flickering drastically, thus allowing you more flexibility over the refresh rate.
Stargazer712
A: 

I would personally suggest investigating the Trident animation library, even if you are using it in a mobile context (as it seems from the question you are) the library appears to be only about 100k.

I'm not sure how well suited it is to your situation, however it is worth a try.

And on a nitpicky point...the byte datatype in Java is not unsigned, so its range is actually -128 to 127.

Thomas