我需要根据 HashMap 重新着色位图,其中键是源颜色,值是目标颜色。
我通过迭代像素获得结果:
public void recolor(Bitmap origBitmap, Bitmap newBitmap, Map<Integer, Integer> colorReplace) {
for (x = 0; x < origBitmap.getWidth(); x++) {
for (y = 0; y < origBitmap.getHeight(); y++ {
newBitmap.setPixel(x, y, colorReplace.get(origBitmap.getPixel(x, y)));
}
}
}
它看起来很完美,但即使在超频 i7 CPU 上运行的模拟器上运行速度也有点慢。在真实设备上运行速度非常慢,有时还不稳定。
我寻找了一些让它更快、更稳定的方法。我认为可以通过 PorterDuffColorFilter 或其他 ColorFilter 来完成,但不明白如何实现。
感谢您的建议!
最佳答案
感谢评论中的建议!我找到了一个运行速度足够快(在我的特定情况下约为 70 毫秒,而不是 3500 毫秒)且稳定的解决方案。
public void recolor(Bitmap origBitmap, Bitmap newBitmap, Map<Integer, Integer> colorReplace) {
final width = origBitmap.getWidth();
final height = origBitmap.getHeight();
final int[] pixels = new int[width * height];
origBitmap.getPixels(pixels, 0, width, 0, 0, width, height);
for (i = 0; i < pixels.length; i++) {
pixels[i] = colorReplace.get(pixels[i])
}
newBitmap.setPixels(pixels, 0, width,0, 0, width, height);
}
关于java - 快速重新着色位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50313771/