使用java旋转gif文件格式图像

时间:2013-12-12 11:57:05

标签: java image animation rotation gif

我想旋转我的gif图像90度并将其显示在一个框架中。我曾尝试在内置类中使用 AffineTransform() AffineTransformOP() 。它的适用于非动画图像,但不适用于gif图像。

我还查看了链接Rotate GIF images in java及其评论,它从未帮助我解决我的问题。

所以,请帮助我。

1 个答案:

答案 0 :(得分:0)

你可以设置正确的affineTransform,但你一定要小心!

public static void main(String[] args) throws MalformedURLException {

File file = new File("img/ani.gif");
    URL url = file.toURI().toURL();
    Icon icon = new ImageIcon(url);
    final AffineTransform rot = AffineTransform.getRotateInstance(0.2);
    JLabel label = new JLabel(icon){

        @Override
        public void paint(Graphics arg0) {

           Graphics2D gr = (Graphics2D) arg0;
           AffineTransform original = gr.getTransform();        
           gr.setTransform(rot);
           super.paint(arg0);
           gr.setTransform(original);
        }           
    };        

    JFrame f = new JFrame("Animation");
    f.getContentPane().add(label);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
}

那么,这段代码做了什么?

在Label开始任何绘图之前,我们得到Graphics并设置affinetrans(rot 0.2) 然后,图形有这个'设置'来绘制动画。 绘图后我们返回'原始'仿射变换。 你应该能够在其他组件上做到这一点......

重要提示

来自:http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html#setTransform%28java.awt.geom.AffineTransform%29

警告:永远不应该使用此方法在现有变换之上应用新的坐标变换,因为Graphics2D可能已经具有其他目的所需的变换,例如渲染Swing组件或应用缩放变换来调整用于解决打印机问题。