Android - VectorDrawable to Bitmap without antialiasing

Viewed 29

I'm trying to convert a VectorDrawable into a Bitmap, by doing the following:

    public static Bitmap vectorToBitmap(VectorDrawableCompat vector) {
        if (vector == null) {
            return null;
        }
        int w = vector.getIntrinsicWidth();
        int h = vector.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        boolean aa = Settings.getBoolean(Settings.Keys.ANTI_ALIASING, false);
        canvas.setDrawFilter(new PaintFlagsDrawFilter(aa ? 0 : Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG |
                                                                Paint.DITHER_FLAG, 0));
        vector.setBounds(0, 0, w, h);
        vector.setFilterBitmap(aa);
        vector.draw(canvas);
        return bitmap;
    }

It works, but the issue is, even when disabling all sorts of antialiasing, the final Bitmap still has it. As a proof example, I only used two colors in a certain SVG, but when the file was converted, my program detected dozens of different colors (as well as the anti-aliasing being clearly visible when zoomed in).

It unfortunately needs to be disabled for my app to work properly. I've looked into many posts, like this one, which states that it could be Bilinear Filtration. The problem is that I can't rasterize the layer because the issue isn't the view, but rather the bitmap itself, which then gets projected onto a GLSurfaceView.

I've already tried looking into the methods of VectorDrawable and Canvas, and none seemed to do the trick. Is there any way to achieve this?

Screenshot of what I mean:

Antialiased picture

Update:

I tried looking into the inner workings of VectorDrawableCompat. It seems like it has two different drawing approaches. The first is when the vector's bitmap is cached, and it draws it using Canvas#drawBitmap(). The second is when the VectorDrawableCompat has a delegate drawable, and it goes to native rendering.

Using a bit of reflection, I saw that in my case the latter was happening, which probably means that my chances here to solve this are low. So either I convert all my SVGs to PNG files (which are fixed bitmaps and I won't need to worry about antialiasing anymore), or try to fuss with filters and stuff. I've also seen that it's impossible to override any method of VectorDrawableCompat since it doesn't have any public/protected constructors...

0 Answers
Related