本文介绍了更改图像透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个项目,我想同时调整和改变图像的透明度。到目前为止,我想我已经得到了大小调整下来。我用像这样定义一个方法来完成调整大小:

 公共BufferedImage的resizeImage(BufferedImage的originalImage,整型){    initialWidth + = 10;
    initialHeight + = 10;
    BufferedImage的resizedImage =新的BufferedImage(initialWidth,initialHeight,类型);
    Graphics2D的G = resizedImage.createGraphics();
    g.drawImage(originalImage,0,0,initialWidth,initialHeight,NULL);
    g.dispose();    返回resizedImage;
}

我得到这个code从这里开始。我无法找到一个解决方案是改变不透明度。这就是我想知道怎么做(如果有可能的话)。先谢谢了。

更新

我想这code,以显示与透明的内侧和外侧圆的图片(见下图)成长,变得越来越少不透明的,但没有奏效。我不知道什么是错的。所有code是在一个名为动画类

 公共动画()抛出IOException    图像= ImageIO.read(新文件(circleAnimation.png));
    initialWidth = 50;
    initialHeight = 50;
    透明度= 1;
}公众的BufferedImage animateCircle(BufferedImage的originalImage,整型){      //不透明度指数下降
      混浊* = 0.8;
      initialWidth + = 10;
      initialHeight + = 10;      BufferedImage的resizedImage =新的BufferedImage(initialWidth,initialHeight,类型);
      Graphics2D的G = resizedImage.createGraphics();
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,不透明度));
      g.drawImage(originalImage,0,0,initialWidth,initialHeight,NULL);
      g.dispose();      返回resizedImage;}

我把它称为是这样的:

 动画动画=新的动画();
整型= animate.image.getType()== 0? BufferedImage.TYPE_INT_ARGB:animate.image.getType();
的BufferedImage newImage;
而(animate.opacity大于0){    newImage = animate.animateCircle(animate.image,类型);
    g.drawImage(newImage,400,350,这一点);}


解决方案

首先确保你传递到到方法的类型包含alpha通道,像

  BufferedImage.TYPE_INT_ARGB

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

 浮动不透明度= 0.5F;
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,不透明度));

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

In a project, I want to simultaneously resize and change the opacity of an image. So far I think I've got the resizing down. I use a method defined like so to accomplish the resizing:

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

I got this code from here. What I can't find a solution to is changing the opacity. That's what I'm wondering how to do (if it's possible at all). Thanks in advance.

UPDATE:

I tried this code to display a picture of a circle with transparent insides and outsides (see below image) growing and becoming less and less opaque, but it didn't work. I'm not sure what's wrong. All the code is in a class called 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;

}

I call it like this:

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

}
解决方案

first make sure the type you're passing into to method contains an alpha channel, like

BufferedImage.TYPE_INT_ARGB

and then just before you paint the new image, call the Graphics2D method setComposite like so:

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

that will set the drawing opacity to 50%.

这篇关于更改图像透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:15