Android位图colorfiltering提供不同的输出

时间:2013-07-26 19:27:31

标签: android android-canvas ondraw colormatrix colorfilter

我遇到了一个问题,即使用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的画布。问题是为什么?不应该是相同的,因为应用了相同的过滤器?

更新:我一直在测试,似乎与半透明图像有关。我试图复制完全不透明的图像,这在所有6个案例中都很好。

但回到我的形象,我甚至尝试使用API​​提供的单位矩阵。

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

但结果相同。

0 个答案:

没有答案
相关问题