Palette always generate null swatches

36 Views Asked by At

I want to create a bitmap with any background color. and print title on it. I don't know in advance what the background color will be.
I try to use Palette to generate title text based on background color, but Palette always generate empty swatches.

My code:

bmpImage = Bitmap.createBitmap(1130, 842, Bitmap.Config.ARGB_8888); 
canvas = new Canvas(bmpImage);
canvas.drawColor(backgroundColor);

Palette.from(bmpImage).generate(new Palette.PaletteAsyncListener() {
        @Override
        public void onGenerated(@Nullable Palette palette) {
            List<Palette.Swatch> s = palette.getSwatches();
            int x = palette.getDominantColor(0);//<-- always return 0
            Log.d(TAG,"Swatches size"+s.size());//<-- always return 0 size
            Palette.Swatch vibrantSwatch = palette.getVibrantSwatch();

            if(vibrantSwatch!=null) {// <-- Here ALWAYS null 
                paint.setColor(vibrantSwatch.getBodyTextColor());
                canvas.drawText(setTitle, textX, textY, paint);
            }
        }
    });

My bitmap is correct. I save it by this code bmpImage.compress(Bitmap.CompressFormat.PNG, 100, outpStream); and it succesful save image.
gradle: implementation 'androidx.palette:palette:1.0.0'

So how to correctly use Palette?

0

There are 0 best solutions below