java修改像素颜色_java – 更快速地设置(PNG)位图颜色而不是像素的颜色

本文探讨了如何高效地修改PNG图像的颜色,尤其是针对含有透明度的图像。当前方法通过逐像素设置RGB值,导致效率低下。文章提出问题,寻求更快的替代方案,以避免对大尺寸图像进行慢速的像素遍历操作。
摘要由CSDN通过智能技术生成

我有一些png文件,我正在应用一种颜色.颜色根据用户选择而变化.我通过另一种方法设置的3个RGB值更改颜色. png文件是随机形状,在形状外部具有完全透明.我不想修改透明度,只修改RGB值.目前,我正在逐像素设置RGB值(请参阅下面的代码).

我已经意识到这非常缓慢,并且在应用程序中可能效率不高.有没有更好的方法可以做到这一点?

这是我目前正在做的事情.您可以看到像素阵列对于占据屏幕的相当一部分的图像来说是巨大的:

public void foo(Component component, ComponentColor compColor, int userColor) {

int h = component.getImages().getHeight();

int w = component.getImages().getWidth();

mBitmap = component.getImages().createScaledBitmap(component.getImages(), w, h, true);

int[] pixels = new int[h * w];

//Get all the pixels from the image

mBitmap[index].getPixels(pixels, 0, w, 0, 0, w, h);

//Modify the pixel array to the color the user selected

pixels = changeColor(compColor, pixels);

//Set the image to use the new pixel array

mBitmap[index].setPixels(pixels, 0, w, 0, 0, w, h);

}

public int[] changeColor(ComponentColor compColor, int[] pixels) {

int red = compColor.getRed();

int green = compColor.getGreen();

int blue = compColor.getBlue();

int alpha;

for (int i=0; i < pixels.length; i++) {

alpha = Color.alpha(pixels[i]);

if (alpha != 0) {

pixels[i] = Color.argb(alpha, red, green, blue);

}

}

return pixels;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值