Java 使用函数更改位图颜色

Java 使用函数更改位图颜色,java,android,matrix,bitmap,colormatrix,Java,Android,Matrix,Bitmap,Colormatrix,我需要一个函数,将采取位图,并返回一个改变了颜色的位图。它需要快速和简单 它的目的是改变颜色,也是一个带有字母的png 我在网上看过,但我不能使用画布或任何外部的东西。函数驻留在外部对象中(不询问..) 这是我到目前为止尝试过的(没有工作)。我知道我真的很接近,只是一个整理颜色矩阵和让alpha工作的问题 public Bitmap changeBitmapColor(Bitmap sourceBitmap, int deg) { int width, height; he

我需要一个函数,将采取位图,并返回一个改变了颜色的位图。它需要快速和简单

它的目的是改变颜色,也是一个带有字母的png

我在网上看过,但我不能使用画布或任何外部的东西。函数驻留在外部对象中(不询问..)

这是我到目前为止尝试过的(没有工作)。我知道我真的很接近,只是一个整理颜色矩阵和让alpha工作的问题

public Bitmap changeBitmapColor(Bitmap sourceBitmap, int deg)
{   
    int width, height;
    height = sourceBitmap.getHeight();
    width = sourceBitmap.getWidth();    

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();

    //figure out color matricies.
    ColorMatrix cm = new ColorMatrix();

    //cm.setSaturation(0);

    cm.set(new float[] 
    {
            0, 0, 0, 0, 0,
            0, 1, 0, 0, 0,
            0, 0, 255, 0, 0,
            0, 0, 0, 1, 0,
            0, 0, 0, 0, 1
         }); 


    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);

    c.drawBitmap(sourceBitmap, 0, 0, paint);

    return bmpGrayscale;
}
任何帮助都会很好

-------固定的--------

我已经通过更改颜色矩阵解决了这个问题,现在位图将更改颜色&而不显示alpha值

首先是矩阵:

    cm.set(new float[] 
    {
            0, 0, 0, 0, 0,
            0, 1, 0, 0, 0,
            0, 0, 140, 0, 0,
            0, 0, 0, 1, 0,
            0, 0, 0, 0, 1
    }); 
cm.set(new float[] 
{
        0, 0, 0, 0, 0,
        0, 1, 0, 0, 0,
        0, 0, 140, 0, 0,
        0, 0, 0, 1, 0,
        0, 0, 0, 0, 1
}); 
我必须更改的第二件事是这行代码:

Bitmap newBM = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

祝读者好运

我不明白为什么单独使用就不能使用画布。您可以创建一个新位图,创建一个画布以绘制到其中,然后使用设置了颜色过滤器的绘制(使用
setColorFilter
)绘制原始位图。该类可能对此有所帮助。

我不明白更改颜色的目的是什么,以及参数
int deg
的含义是什么??对于alpha方面:将
位图bmpGrayscale
中的颜色模型从
RGB_565
更改为
ARGB_8888
,这可能会有所帮助,因为该模型具有alpha通道

----固定--------

我已经通过更改颜色矩阵解决了这个问题,现在位图将更改颜色&而不显示alpha值

首先是矩阵:

    cm.set(new float[] 
    {
            0, 0, 0, 0, 0,
            0, 1, 0, 0, 0,
            0, 0, 140, 0, 0,
            0, 0, 0, 1, 0,
            0, 0, 0, 0, 1
    }); 
cm.set(new float[] 
{
        0, 0, 0, 0, 0,
        0, 1, 0, 0, 0,
        0, 0, 140, 0, 0,
        0, 0, 0, 1, 0,
        0, 0, 0, 0, 1
}); 
我必须更改的第二件事是这行代码:

Bitmap newBM = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Bitmap newBM=Bitmap.createBitmap(宽度、高度、Bitmap.Config.ARGB_8888)


祝读者好运

你还没有说清楚你想在颜色上做什么改变。您定义的
cm
矩阵看起来有一个额外的行,它删除了红色,只保留了绿色和alpha,并将蓝色乘以255。我猜那不是你想要的

我要去的地方是

如果您尝试旋转色调,您可能会从以下答案中获得一些好信息:

谢谢你的帮助。我想出来了!:-)