2013-02-24 226 views
3

我有,我想在java中来调整,但试图使用传统方法时,它似乎不工作的动画.GIF图像:调整大小。GIF图像

ImageIcon esclamativoMid = null; 
//... search my file... 
if(name.equalsIgnoreCase("esclamativo.gif")) 
    esclamativoMid = new ImageIcon(f.getAbsolutePath()); 
myEnumMap.put(Resolution.MID, esclamativoMid); 

for(Resolution r: Resolution.values()){ 
    if(r != Resolution.MID){ 
     int w = esclamativoMid.getIconWidth()*(r.ordinal()+1)/2; 
     int h = esclamativoMid.getIconHeight()*(r.ordinal()+1)/2; 
     BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 
     Graphics2D g2d = (Graphics2D)bi.createGraphics(); 
     g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); 
     g2d.drawImage(esclamativoMid.getImage(), 0, 0, w, h, null); 
     myEnumMap.put(r, new ImageIcon(bi)); 
    } 
} 

当我尝试显示图像使用代码:

new JLabel(myEnumMap.get(currentRes)); 

我得到.GIF仅当Resolution.MID(IE的图像直接从文件加载)。

回答

1

Java有一个简单的图像缩放库叫imgscalr

这个库实现了几种不同的图像缩放方法,如果你问它或者给你最快或最好看的(如果你要求的话),它会为你选择最优方法。

用法很简单,只是一堆静态方法。最简单的用例是:

BufferedImage scaledImage = Scalr.resize(myImage, 200); 

所有操作保持图像的原始比例,所以在这种情况下,你是问imgscalr宽高,默认为界200个像素和200个像素内重新大小的图像它会自动选择最好和最快的方法,因为它没有被指定。

+2

我试过了,对其他图像看起来不错,但'.GIF'图像是固定的:( – Giudark 2013-02-25 02:37:50