Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i convert to Color to Bitmap?

I have a color in form of integer, and i want that color to be in a Bitmap form.
Is there any way to do so?

I tried

Drawable d = new ColorDrawable(Color.parseColor("#ffffff"));
Bitmap b = ((BitmapDrawable)d).getbitmap();

But the above code give error Cannot cast ColorDrawable to BitmapDrawable

Is there any other way?

Actual code is

Palette.generateAsync(BitmapFactory.decodeFile(songArt),
            new Palette.PaletteAsyncListener() {
                @Override
                public void onGenerated(final Palette palette) {
                    if (Build.VERSION.SDK_INT >= 16) {
                        Drawable colorDrawable = new ColorDrawable(palette.getDarkVibrantColor(
                                getResources().getColor(R.color.noti_background)));
                        notificationCompat.bigContentView.setImageViewResource(R.id.noti_color_bg,
                                ((BitmapDrawable) colorDrawable).getBitmap());
                        notificationManager.notify(NOTIFICATION_ID, notificationCompat);
                    }
                }
            }
    );
like image 591
architjn Avatar asked Jul 05 '15 16:07

architjn


1 Answers

Yes there is. You can just do it like this:

Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawColor(colorInt)

Inside drawColor() you can also set the color by using the methods of the Color class like Color.argb(...) or Color.rgb(...)

That way you will have a bitmap with dimensions width/height and filled with the specified color.

Further explanation: You create a Bitmap object with your specified dimensions. Then you create a Canvas object and attach the Bitmap object to it. This way everything that is drawn using the Canvas object methods gets drawn on the Bitmap object.

In the end you have a Bitmap object with your drawings in it.

like image 179
Ilja KO Avatar answered Oct 20 '22 18:10

Ilja KO