2013-02-24 154 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