问题描述
我正在制作一个游戏,并希望有一个图像从左到右淡入淡出",图像的左侧部分的 alpha 为 1.0,右侧的 alpha 为 0.0.(注意:我不希望它随着时间的推移而改变它的样子,比如淡入或淡出,而只是从左到右淡出并保持不变).尝试绘制我想要的最终结果如下所示:
I am making a game and want to have a single image 'fade' from left to right with the left part of the image having an alpha of 1.0 and the right having an alpha of 0.0. (note: I do not want it to be changing what it looks like over time, like fading in or out, but just fading from left to right and staying constant). An attempt to draw what I want the end result to look like is below:
lll lll ll ll l l l l l
lll lll ll ll l l l l l
lll lll ll ll l l l l l
lll lll ll ll l l l l l
lll lll ll ll l l l l l
lll lll ll ll l l l l l
'l's 的密度代表 alpha
Where the densities of the 'l's represent the alpha
我目前正在使用 TYPE_INT_RGB 的缓冲图像,并希望尽可能保持相同.
I am currently using Buffered Images of TYPE_INT_RGB, and would like to keep that the same if possible.
是否有任何内置的 java 类可以帮助我做到这一点,或者至少有一种(相对简单的)方法可以让我自己做到这一点,但我无法弄清楚?
Is there any built in java classes that can help me do this, or at least a (relatively easy) way to do this myself that I just can't figure out?
我不想有任何形式的不透明框架.我想将一个 BufferedImage(带有 alpha 渐变)绘制到另一个 BufferedImage 上.
I don't want to have an opaque frame of any sort. I want to draw one BufferedImage (with alpha gradient) onto another BufferedImage.
推荐答案
基本思想是将 AlphaComposite
蒙版应用到已用 LinearGradientPaint
The basic idea is to apply a AlphaComposite
mask over the original image which has been filled with a LinearGradientPaint
所以,我们首先加载原始图像...
So, we start by loading the original image...
BufferedImage original = ImageIO.read(new File("/an/image/somewhere"));
然后我们创建一个相同大小的遮罩图像...
We then create a masking image of the same size...
BufferedImage alphaMask = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_ARGB);
然后我们用 LinearGradientPaint
...
Graphics2D g2d = alphaMask.createGraphics();
LinearGradientPaint lgp = new LinearGradientPaint(
new Point(0, 0),
new Point(alphaMask.getWidth(), 0),
new float[]{0, 1},
new Color[]{new Color(0, 0, 0, 255), new Color(0, 0, 0 , 0)});
g2d.setPaint(lgp);
g2d.fillRect(0, 0, alphaMask.getWidth(), alphaMask.getHeight());
g2d.dispose();
这里重要的是,我们实际上并不关心物理颜色,只关心它的 alpha 属性,因为这将决定两个图像如何被屏蔽在一起......
What's important here is, we don't actually care about the physical color, only it's alpha property, as this will determine how the two images are masked together...
然后,我们应用蒙版...
Then, we apply the mask...
BufferedImage faded = applyMask(original, alphaMask, AlphaComposite.DST_IN);
实际上调用了这个实用方法...
Which actually calls this utility method...
public static BufferedImage applyMask(BufferedImage sourceImage, BufferedImage maskImage, int method) {
BufferedImage maskedImage = null;
if (sourceImage != null) {
int width = maskImage.getWidth();
int height = maskImage.getHeight();
maskedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D mg = maskedImage.createGraphics();
int x = (width - sourceImage.getWidth()) / 2;
int y = (height - sourceImage.getHeight()) / 2;
mg.drawImage(sourceImage, x, y, null);
mg.setComposite(AlphaComposite.getInstance(method));
mg.drawImage(maskImage, 0, 0, null);
mg.dispose();
}
return maskedImage;
}
这基本上是使用 AlphaComposite
中的目的地"将蒙版应用到原始图像上,从而导致...
This basically uses a "destination in" AlphaComposite
to apply the mask onto the original image, which results in...
(左边原始,右边 alpha)
(original on the left, alpha on the right)
为了证明这一点,我将框架内容窗格的背景颜色更改为 RED
And just to proof the point, I changed the background color of the frame's content pane to RED
这篇关于我可以在 Java 中让图像 alpha 从左到右淡出吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!