How do i convert to Color to Bitmap?
Asked Answered
O

1

11

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);
                    }
                }
            }
    );
Oto answered 5/7, 2015 at 16:47 Comment(0)
C
27

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.

Covarrubias answered 5/7, 2015 at 16:54 Comment(10)
you should only be setting one colour, in the draw(color) command... You have wrriten your answer very misleadinglyElspet
edited just wanted to point out various ways to set a color ssince he is parsing the color and its an odd way for me to do it like this somehowCovarrubias
This is a better way of doing what he is trying to achieve in the question.Elspet
he says he has the color integer allready so why parsing from a string?Covarrubias
@IljaKO its working but the problem is how do i get the height and width?Oto
do you allready have an existing bitmap? then just use this instead of making a new one. Otherwise its up to you how big you want your bitmap...bitmap class has also methods like getWidth() or getHeight()...ps youre free to mark my answer as correct when it works;)Covarrubias
developer.android.com/reference/android/graphics/Bitmap.html developer.android.com/reference/android/graphics/Canvas.htmlCovarrubias
@IljaKO everything worked perfect , i stretched the bitmap to fit imageviewOto
great...did you mean by stretching that you scaled the bitmap?Covarrubias
you could also just create a bitmap like this when you wan to have it the dimensions of your image view: Bitmap.createBitmap(imageView.getWidth(),imageView.getHeight(),Config.ARGB_8888);Covarrubias

© 2022 - 2024 — McMap. All rights reserved.