androidmupdf

mupdf: android library: How to invert the colors or change to night mode


If anyone using mupdf library for android know how to invert the background. I want to add a button to the ui


Solution

  • It's very simple. In PageView while rendering bitmap just invert the bitmap.

    mEntire.setImageBitmap(invert(mEntireBm));
    mPatch.setImageBitmap(invert(mPatchBm));
    

    Here is how you can invert a bitmap.

        private Bitmap invert(Bitmap src) {
            int height = src.getHeight();
            int width = src.getWidth();
    
            Bitmap bitmap = Bitmap.createBitmap(width, height, src.getConfig());
            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);
            // src.recycle();
            return bitmap;
        }