更快的方式通过像素设置像素的(PNG)位图颜色,而不是 [英] Faster way to set a (PNG) bitmap color instead of pixel by pixel

查看:117
本文介绍了更快的方式通过像素设置像素的(PNG)位图颜色,而不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我申请一个颜色一些PNG文件。颜色的变化取决于用户的选择。我通过改变从另一种方法设置3 RGB值的颜色。该PNG文件是随机的形状与形状外完全透明。我不希望修改透明度,只有RGB值。目前,我设置RGB值逐像素(见下文code)。

我已经认识到,这是令人难以置信的速度慢,可能只是没有足够的效率做一个应用程序。有没有更好的办法,我能做到这一点?

下面是我目前做的事情。你可以看到,像素阵列是巨大的一个形象,占据了屏幕的体面的一部分:

 公共无效美孚(色差分量,ComponentColor compColor,INT userColor){
    。INT H = component.getImages()的getHeight();
    。INT W = component.getImages()的getWidth();
    mBitmap = component.getImages()createScaledBitmap(component.getImages(),W,H,真)。

    INT []像素=新INT [H * W];

    //从图像中的所有像素
    mBitmap [指数] .getPixels(像素,0,瓦特,0,0,W,H);

    //修改像素阵列到用户所选择的颜色
    像素= changeColor(compColor,像素);

    //设置图像使用新的像素阵列
    mBitmap [指数] .setPixels(像素,0,瓦特,0,0,W,H);
}

公众诠释[] changeColor(ComponentColor compColor,INT []像素){
    INT红= compColor.getRed();
    INT绿色= compColor.getGreen();
    INT蓝色= compColor.getBlue();
    INT字母;

    的for(int i = 0; I< pixels.length;我++){
        阿尔法= Color.alpha(像素[I]);
        如果(阿尔法!= 0){
            像素[I] = Color.argb(α,红,绿,蓝);
        }
    }
    返回像素;
}
 

解决方案

你看在的位图?像 extractAlpha 听起来像它可能是有用的。你的也顺便看看功能,如在Android的实施,看看你如何能使其适应您的特殊情况下,如果它不能完全满足您的需求。

I have some png files that I am applying a color to. The color changes depending on a user selection. I change the color via 3 RGB values set from another method. The png files are a random shape with full transparency outside the shape. I don't want to modify the transparency, only the RGB value. Currently, I'm setting the RGB values pixel by pixel (see code below).

I've come to realize this is incredibly slow and possibly just not efficient enough do in an application. Is there a better way I could do this?

Here is what I am currently doing. You can see that the pixel array is enormous for an image that takes up a decent part of the screen:

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

解决方案

Have you looked at the functions available in Bitmap? Something like extractAlpha sounds like it might be useful. You an also look at the way functions like that are implemented in Android to see how you could adapt it to your particular case, if it doesn't exactly meet your needs.

这篇关于更快的方式通过像素设置像素的(PNG)位图颜色,而不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆