更改图像不透明度
在项目中,我想同时调整图像的大小和更改图像的不透明度。到目前为止,我认为我已经缩小了大小。我使用这样定义的方法来完成大小调整:
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;
}
我从这里得到了这个代码。我找不到解决方案的是改变不透明度。这就是我想知道如何做的(如果可能的话)。提前致谢。
更新:
我尝试了这段代码来显示一张带有透明内部和外部(见下图)的圆圈的图片,这些圆圈变得越来越不透明,但它不起作用。我不确定出了什么问题。所有代码都在一个名为Animation的类中。
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);
}