更改图像不透明度

帕萨瓦(Pasawaya):

在项目中,我想同时调整大小和更改图像的不透明度。到目前为止,我认为我已经缩小了尺寸。我使用这样定义的方法来完成大小调整:

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);

}
tommo:

首先确保要传递给方法的类型包含一个alpha通道,例如

BufferedImage.TYPE_INT_ARGB

然后在绘制新图像之前,像这样调用Graphics2D方法setComposite:

float opacity = 0.5f;
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));

将绘图不透明度设置为50%。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用鼠标指针下的JS或CSS更改图像不透明度?

使用CSS更改背景图像的不透明度

如何更改背景图像的不透明度?

更改不透明度值后,图像仍显示半透明不透明度

使用GetElementById时更改图像不透明度

jQuery更改图像不透明度onclick

使用Tampermonkey更改HTML图像的不透明度

如何更改在KML文档中作为图标导入的图像的不透明度?

包含文本的图像的包装div不能同时更改图像的亮度滤镜和文本的不透明度滤镜

如何在我的传单代码中修改图像的不透明度

通过过渡更改图像不透明度

如何使用CSS更改悬停时图像的不透明度

在div中其他元素悬停时更改图像不透明度

通过更改mousemove事件上的封面图像不透明度来发现隐藏的图像

使用imagick更改图像的不透明度

根据页面宽度更改背景图像的不透明度

如何在MS Word 2007中更改图像的不透明度?

悬停文本时如何更改图像的不透明度?

通过更改不透明度来突出显示图像

如何使用Bootstrap的轮播组件更改图像的不透明度,而不更改覆盖文本

如何使用画布更改图像的不透明度,然后将其加载到画布中的另一图像上?-HTML

如何更改正文标签背景图像的不透明度

更改多个图像的不透明度 Xamarin.Forms

如何使用 camanjs 更改图像不透明度?

GdkPixbuf 更改不透明度

使用js更改背景图像不透明度而不影响前景图像

如何:单选按钮更改图像的不透明度

更改图标的不透明度

仅更改所有图像的不透明度,除了悬停的图像