java如何使用不透明度绘制渐变
我有这个方法,它可以生成一个渐变,但由于某种原因,我无法使渐变具有任何不透明度,例如60% opaque
public static int[] linear(int x1, int y1, int x2, int y2, Color color1, Color color2, int width, int height){
BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
int[] pixels = new int[width * height];
Graphics2D g = bimg.createGraphics();
g.setPaint(new GradientPaint(x1, y1, color1, x2, y2, color2, false));
g.fillRect(0, 0, width, height);
bimg.getRGB(0, 0, width, height, pixels, 0, width);
return pixels;
}
然后我这样称呼它:
int pink = Colors.rgba(187, 61, 186, 153);
int yellow = Colors.rgba(209, 192, 8, 153);
this.spixels = Gradient.linear(0, 0, img.getWidth(), 0, pink, yellow, img.getWidth(), img.getHeight());
我一生都无法得到60% opaque
的梯度。我该怎么做才能做到这一点
以下是更多的背景信息:
我有一个图像,然后创建一个与图像大小相同的渐变(使用上面的代码)。接下来,我使用lighten
将两个图像混合在一起:
public static int lighten(int bg, int fg){
Color bgc = new Color(bg);
Color fgc = new Color(fg);
int r = Math.max(bgc.getRed(), fgc.getRed());
int g = Math.max(bgc.getGreen(), fgc.getGreen());
int b = Math.max(bgc.getBlue(), fgc.getBlue());
int a = Math.max(bgc.getTransparency(), fgc.getTransparency());
Color f = new Color(r, g, b, a);
return f.getRGB();
}
无论我使渐变变得多么透明,Lighting似乎无法捕捉它,并将其与全色混合,忽略渐变的透明度
# 1 楼答案
像这样定义一个
Composite
对象在
linear()
方法中,只需在g.fillRect()
之前设置组合下面的代码片段,在我的一个代码的绘制方法中演示了类似的东西