我遇到一个问题,使用ColorMatrixColorFilter提供不同的输出 . 我有这张照片:
enter image description here

它有白色,红色和透明的部分 . 我有一个视图,覆盖了onDraw(Canvas)方法,我绘制了这张图片 .

@Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(r, 0, 0, null);
        canvas.drawBitmap(r, r.getWidth(), 0, blank);
        canvas.drawBitmap(r, r.getWidth() * 2, 0, identity);//darker why?
        canvas.drawBitmap(identityfiltered, r.getWidth() * 3, 0, null);
        canvas.drawBitmap(identityfiltered, r.getWidth() * 4, 0, blank);
        canvas.drawBitmap(identityfiltered, r.getWidth() * 5, 0, identity);//darker why?
    }

其他变量解释如下:

Paint blank = new Paint(); //blank paint

ColorMatrixColorFilter filter = new ColorMatrixColorFilter(new float[] { 
                    1, 0f, 0f, 0f, 0f, // red
                    0, 1f, 0f, 0f, 0f, // green
                    0, 0f, 1f, 0f, 0f, // blue
                    0, 0f, 0f, 1f, 0f, // alpha
            });
Paint identity = new Paint();
identity.setColorFilter(filter); //the paint with identity filter

Bitmap r = BitmapFactory.decodeResource(getResources(), R.drawable.my_drawable);// my drawable

Bitmap identityfiltered = Bitmap.createBitmap(r.getWidth(), r.getHeight(), Config.ARGB_8888); //new bitmap with the same width height as my drawable, Bitmap r is drawn on this with the identity filter
Canvas c = new Canvas(identityfiltered);
c.drawBitmap(r, 0, 0, identity);

给定的输出是:

enter image description here

正如你所看到的那样,第3和第6个绘制的位图更暗,就是当我用colorfilter直接绘制到View的画布时 . 问题是为什么?不应该是相同的,因为应用相同的过滤器?

更新:我一直在测试, seems like it has something to do with semi-transparent 图像 . 我试图复制完全不透明的图像,这在所有6个案例中都很好 .

但回到我的图像,我甚至尝试使用API给出的单位矩阵 .

The new ColorMatrix() constructor comes with identity matrix initialized.

但结果相同 .