Java控制动画——如何让它们流畅高效

发布于 2024-09-05 07:37:56 字数 651 浏览 2 评论 0原文

我正在用 Java 编写一些自定义控件,并使用动画进行过渡/淡入淡出/移动。

我这样做的方式是启动一个新线程并对变量进行调整,并使用 Paint() 方法进行调整。

示例:

现在,我的问题是,假设我正在实现淡入。我要做的是将 alpha 变量增加 byte x // Between 0-255 并绘制一个矩形,其中alphaLevel += x,例如(在伪代码中):

byte increment = 40;

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

_parentClass.setAlphaLevel (255);

我想知道的是我应该将睡眠设置为最低值和最高值,这样动画就不会看起来断断续续?它与目标设备刷新率或人眼有什么关系吗?同样的问题再次与步骤。有没有一个网站可以给我提供可以复制的好数据。

我问的原因是为了最大限度地提高效率,因为它将在电池供电的设备上运行,因此更多的 CPU 时间 = 更少的电池。你会把它设置成什么?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无语# 2024-09-12 07:37:56

您正在寻找的技术称为“双缓冲”。

遗憾的是,我没有时间向您展示示例,但这就是您需要研究的内容。

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.

笑看君怀她人 2024-09-12 07:37:56

我个人建议研究 Trident 动画库,即使您在移动上下文(从你的问题看来)该库似乎只有大约 100k。

我不确定它是否适合您的情况,但值得一试。

需要指出的是,Java 中的 byte 数据类型不是无符号的,因此它的范围实际上是 -128 到 127。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文