在項目中,我想同時調整大小並更改圖像的不透明度。到目前爲止,我認爲我已經調整了大小。我使用如此定義的方法來完成調整大小:更改圖片不透明度
public BufferedImage resizeImage(BufferedImage originalImage, int type){
initialWidth += 10;
initialHeight += 10;
BufferedImage resizedImage = new BufferedImage(initialWidth, initialHeight, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, initialWidth, initialHeight, null);
g.dispose();
return resizedImage;
}
我從這裏得到了這段代碼。我找不到解決方案是改變不透明度。這就是我想知道如何去做(如果可能的話)。提前致謝。
UPDATE:
我嘗試這樣做的代碼,以顯示與透明內側和外側的圓的圖像(見下圖)生長和越來越低不透明的,但它沒有工作。我不確定有什麼問題。所有的代碼是在一個名爲動畫
public Animation() throws IOException{
image = ImageIO.read(new File("circleAnimation.png"));
initialWidth = 50;
initialHeight = 50;
opacity = 1;
}
public BufferedImage animateCircle(BufferedImage originalImage, int type){
//The opacity exponentially decreases
opacity *= 0.8;
initialWidth += 10;
initialHeight += 10;
BufferedImage resizedImage = new BufferedImage(initialWidth, initialHeight, type);
Graphics2D g = resizedImage.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
g.drawImage(originalImage, 0, 0, initialWidth, initialHeight, null);
g.dispose();
return resizedImage;
}
我這樣調用類:
Animation animate = new Animation();
int type = animate.image.getType() == 0? BufferedImage.TYPE_INT_ARGB : animate.image.getType();
BufferedImage newImage;
while(animate.opacity > 0){
newImage = animate.animateCircle(animate.image, type);
g.drawImage(newImage, 400, 350, this);
}
爲了更快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2012-07-19 02:29:25