Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 快速重着色位图_Java_Android - Fatal编程技术网

Java 快速重着色位图

Java 快速重着色位图,java,android,Java,Android,我需要根据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(

我需要根据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)));
        }
    }
}
public void recolor(位图origBitmap、位图newBitmap、贴图颜色替换){
对于(x=0;x
它看起来很完美,但即使在超频i7cpu上运行的模拟器上也运行得很慢。在实际设备上运行得很慢,有时甚至不稳定

我寻找了一些方法使它更快更稳定。我认为它可以通过PorterDuffColorFilter或其他颜色过滤器来实现,但不知道如何实现


谢谢你的建议!

谢谢你在评论中的建议!我找到了一个工作速度足够快(大约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);
}
public void recolor(位图origBitmap、位图newBitmap、贴图颜色替换){
最终宽度=origBitmap.getWidth();
最终高度=origBitmap.getHeight();
最终整数[]像素=新整数[宽度*高度];
getPixels(像素,0,宽度,0,0,宽度,高度);
对于(i=0;i
请参见
位图#getPixels
/
位图#setPixels
setPixel是最糟糕的方法。您甚至最好获取完整的像素数据数组并直接对其进行变异。但是PorterDuff和ColorFilter都不会帮助您,也不会在HashMap中用任意值替换colros。仅供参考,您的算法将是c如果源像素的值不在地图中,则会出现问题。不确定代码的其余部分是否会阻止这种情况,但我认为应该警告您。@Gabeschen HashMap包含所有颜色,因为它是由迭代原始图像生成的(我看到getPixel比setPixel快得多)而且在模拟器中总是有效的。谢谢,我将尝试在阵列中获取像素,或者您可以切换到“疯子模式”并使用,但我怀疑它会比
getPixels
/
setPixels
快得多,并用
像素[1]
替换
像素[I]
@pskink是的,当然,更正示例格式时出现奇怪的打字错误BTW尝试了jni和/或70毫秒就足够了?70毫秒对于我的需要来说真的很酷。有了这个速度,它可以在没有单独线程的情况下完成,但由于一些未知的原因(可能对于速度非常慢的设备),我在单独的线程中运行它。:)