我可以在java中从左到右进行图像alpha淡化吗? [英] Can I have image alpha fade from left to right in java?

查看:161
本文介绍了我可以在java中从左到右进行图像alpha淡化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作游戏,并希望从左到右让单个图像淡入淡出,图像的左侧部分的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'的密度代表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上绘制一个BufferedImage(带有alpha渐变)。

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淡化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆