Search code examples
javaandroidandroid-graphics

Android PixelXorXfermode deprecated


I have a old library that used PixelXorXfermode class. But it was deprecated in API 17 and removed from API 24.

paint.setXfermode(new PixelXorXfermode(-1));

I tried to look at its source code and see it call native method.

private static native long nativeCreate(int opColor);

Is there any alternative for this class? Or is there any way to port it?


Solution

  • I found a good tutorial here that explains how to use ColorFilters with lots of examples. The "invert" sample seems accomplish the same thing as PixelXorXfermode.

    public Bitmap getBitmap(Bitmap original)
    {
        Bitmap bitmap = Bitmap.createBitmap(original.getWidth(), 
        original.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
    
        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(getColorMatrix()));
        canvas.drawBitmap(original, 0, 0, paint);
    
        return bitmap;
    }
    
    private ColorMatrix getColorMatrix() {
        return new ColorMatrix(new float[] {
            -1,  0,  0,  0, 255,
            0, -1,  0,  0, 255,
            0,  0, -1,  0, 255,
            0,  0,  0,  1,   0
        });
    }