Skip to content

Instantly share code, notes, and snippets.

@moneytoo
Created May 23, 2015 18:39
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moneytoo/87e3772c821cb1e86415 to your computer and use it in GitHub Desktop.
Save moneytoo/87e3772c821cb1e86415 to your computer and use it in GitHub Desktop.
Efficiently invert bitmap on Android
public Bitmap invert(Bitmap src)
{
int height = src.getHeight();
int width = src.getWidth();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
ColorMatrix matrixGrayscale = new ColorMatrix();
matrixGrayscale.setSaturation(0);
ColorMatrix matrixInvert = new ColorMatrix();
matrixInvert.set(new float[]
{
-1.0f, 0.0f, 0.0f, 0.0f, 255.0f,
0.0f, -1.0f, 0.0f, 0.0f, 255.0f,
0.0f, 0.0f, -1.0f, 0.0f, 255.0f,
0.0f, 0.0f, 0.0f, 1.0f, 0.0f
});
matrixInvert.preConcat(matrixGrayscale);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrixInvert);
paint.setColorFilter(filter);
canvas.drawBitmap(src, 0, 0, paint);
return bitmap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment