java - 动画缩放图片

标签 java swing animation

我正在尝试在缩放图片时实现动画 - 即制作一些帧并用计时器显示它们,以便图片看起来正在移动到缩放区域。问题是我不知道该怎么做。 这是一些代码。

if (e.getSource() == timer) {
            currentFrame++;
            int newImageWidth =  ((mandel.getWidth() - selection.width) / frames) ;
            int newImageHeight = ((mandel.getHeight() - selection.height)/frames) ;
            BufferedImage resizedImage = new BufferedImage(newImageWidth , newImageHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = resizedImage.createGraphics();
            g.scale(newImageWidth, newImageHeight);
            g.drawImage(mandelImage, 0, 0, mandel.getWidth(), mandel.getHeight(), null);
            g.dispose();
            mandelImage = resizedImage;//mandelimage is the original picture

            mandel.repaint();//the panel where is the picture

            if (currentFrame >= frames) {

                timer.stop();
                recreate();//the final result on the zoom

            }

最佳答案

当前图像大小的计算需要取决于动画的当前时间/进度。因此,如果您在计算中不使用 currentFrame,则大小将始终相同,并且没有动画。

具体来说,您可能需要这样的东西来随着时间的推移将图像从非常小缩放到完整尺寸:

int newImageWidth =  ((mandel.getWidth() - selection.width) * currentFrame/frames) ;
int newImageHeight = ((mandel.getHeight() - selection.height) * currentFrame/frames) ;

关于java - 动画缩放图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29034013/

相关文章:

java - 在 JTextField 中粘贴内容时使用什么事件?

android - google plus recyclerView滚动动画

JavaScript if 语句不应该返回 true

java - 在《我的世界》新 1.14.4 锻造版中让自定义剪刀在方 block 上工作时遇到问题

java - 非阻塞 while(true) 循环中的 wait(1) 是否比使用 wait() 和 notify() 更有效?

Java 事件传播停止

google-chrome - 如何解决 Chrome 73 上 SVG 旋转 CSS 中的错误?

java - JmonkeyEngine gradlew 构建不起作用

java - 二叉搜索树中小于给定数量的元素数量

java - 面板不同布局中的 Swing 面板