I am trying to use the ColorMatrixColorFilter to swap the red and blue
colors in a bitmap before drawing it into the Canvas. Unfortunately, it
doesn't seem to work when I use a Bitmap object I create using a Buffer. It
works fine if the Bitmap is loaded from a jpg file but when I use my Bitmap
created from a Buffer object, it doesn't draw anything on the canvas.
So my question is if there is someone special one has to add to a bitmap (a
property, flag, etc) that I need to add to a Bitmap that will allow it to
be rendered on the Canvas using the filter?
I'm using the following code to create the ColorMatrix:
float matrix[] = new float[] {
0, 0, 1, 0, 0,
0, 1, 0, 0, 0,
1, 0, 0, 0, 0,
0, 0, 0, 1, 0
};
rbSwap = new ColorMatrix(matrix);
paint = new Paint(Paint.FILTER_BITMAP_FLAG);
paint.setColorFilter(new ColorMatrixColorFilter(rbSwap));
If I create the bitmap using the following code, it works:
bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.picture);
If I create the bitmap using the following code, nothing is ever drawn to
the canvas:
bitmap = Bitmap.createBitmap((int) width, (int) height,
Bitmap.Config.ARGB_8888);
srcBuffer = ByteBuffer.wrap(data);
srcBuffer.rewind();
bitmap.copyPixelsFromBuffer(srcBuffer);
My onDraw() looks like the following:
public void onDraw(Canvas canvas) {
canvas.drawBitmap(spicebmp, offsetX, offsetY, paint);
}
If I don't set the colorfilter, the bitmap renders on my canvas. If I set
the colorfilter, it is simply a black screen - nothing appears to render.
If I use the jpg bitmap instead of the one I am creating via the buffer, it
draws with the red/blue swapped.
I did the following to debug the problem:
I changed the matrix to be the same as the identity matrix and the bitmap
is rendered properly. If I change any single float in the matrix (like the
1s to .5s or 0), nothing is drawn.
I've also checked to make sure that the bitmap has enough "bytes" to
represent the bitmap. bitmap.byteCount() == srcBuffer.limit() was true - so
the bytes in my buffer were the same as what should be present for the
width/height that I am passing in.
I put a try/catch for any exception during the onDraw as well as poured
through the logcat output, but didn't see anything there. I poked around in
the Android bitmap drawing code and saw the following:
// nothing to draw
if (fClip->isEmpty() ||
bitmap.width() == 0 || bitmap.height() == 0 ||
bitmap.getConfig() == SkBitmap::kNo_Config ||
(origPaint.getAlpha() == 0 && origPaint.getXfermode() == NULL)) {
return;
}
I don't have the ability to trace into the C++ code in the core so I can't
tell if this is triggering. But I do know that my bitmap's width and height
are both non-zero (they are 800 and 600) and I know that my bitmap was set
on creation to be of type ARGV_8888 and the exact same paint object is used
for both a jpg loaded from disk (the one that works) and for the one I
create using the copyPixelsFromBuffer().
So I'm not sure what I am doing wrong. I presume it is some flag or
property in the bitmap that is incompatible with colorFiltering, but it is
just a simple ByteBuffer of the proper size of type ARGB_8888. And of
course, the exact same code (paint object, etc) is used to set up the
filter.
So is there something else I need to be doing with the Bitmap? With the
ColorMatrix/Filter?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en