2010-11-22 58 views
1

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

我已经意识到这是非常慢,可能只是没有足够的效率在应用程序中。有没有更好的方法可以做到这一点?

这是我目前正在做的。你可以看到,像素阵列是巨大的,占用了屏幕的体面部分的图像:

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; 
} 
+1

也许你可以使用一个图像与一个调色板?由于所有内容都使用一种颜色,这意味着无论如何,只能从一个输入中获得256种可能的像素颜色(因为RGB部分将是相同的,并且只有A部分会有所不同)。如果您的图像具有256种不同颜色的调色板,而这些颜色只与alpha通道的值不同,则只需修改调色板,而不是图像数据本身。 – 2010-11-22 23:06:49

+0

我要么不理解你,要么你不了解我的问题。我想修改的每个图像都有2个alpha值,完全透明和半透明/不透明。我想修改RGB值并保持alpha值相同。这就是说,虽然我得到调色板的概念,但在android中没有Palette类。你建议我读什么? – user432209 2010-11-22 23:32:06

回答

2

你看在Bitmap可用的功能?像extractAlpha听起来像它可能是有用的。你也可以看看在Android中实现的功能的方式,看看如果它不能完全满足你的需求,你会如何适应你的特定情况。

1

为我工作的答案是写了广场在这里做Transparent jpegs

他们提供了这样做确切的事情更快的代码段。我尝试了extractAlpha,但它没有工作,但Square的解决方案。只需修改他们的解决方案,而不是修改颜色位而不是alpha位。

pixels[x] = (pixels[x] & 0xFF000000) | (color & 0x00FFFFFF);