开发者

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开发者_C百科 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜