Java – How to create a bitmap from RGB colors

How to create a bitmap from RGB colors… here is a solution to the problem.

How to create a bitmap from RGB colors

I’m making an app that uses 3 SeekBars to allow users to create RGB colors. Then I want to use WallpaperManager to set this color as the user’s background.

If I have 3 values, one for red, one for green, and one for blue, is there a way to create a square bitmap filled with only one of these colors?

Solution

You can do this to create a square bitmap with a selected color.

// Here you create the bound of your shape
Rect rect = new Rect(0, 0, 1, 1);

 You then create a Bitmap and get a canvas to draw into it
Bitmap image = Bitmap.createBitmap(rect.width(), rect.height(), Config.ARGB_8888);
Canvas canvas = new Canvas(image);

You can get an int value representing an argb color by doing so. Put 1 as alpha by default
int color = Color.argb(alpha, red, green, blue);

Paint holds information about how to draw shapes
Paint paint = new Paint();
paint.setColor(color);

 Then draw your shape
canvas.drawRect(rect, paint);

Related Problems and Solutions