Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the app icon color code programmaticallyin android?

Actually My requirement is to get the icon color of any app installed in my device. I want to show an lock screen of that color. So how can I get the color code of any icon programmatically?


1 Answers

if you want to get the all color's RGB value from a single icon--

Bitmap bitmap;
// create  the bitmap from your obtained image
int pixel = bitmap.getPixel(x,y); // x,y is the desired position of the target pixel, for full imag, you have to do the same thing in a loop

int red = Color.red(pixel);
int green = Color.green(pixel);
int blue = Color.blue(pixel);

The int values returned are your standard 0 - 255. You can modify this code and get a color from anywhere, providing you can turn it into a bitmap. And you can use the Color API to get an actual RGB value like this:

int rgb = Color.rgb(red, blue, green); // rgb value of a single pixel, 

Now, in order to get the all the pixels at once, you can use Bitmap.getPixels()

int[] allPixels = new int[bitmap.getWidth()*bitmap.getHeight()];
bitmap.getPixels(allPixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
like image 98
Amit K. Saha Avatar answered Feb 21 '26 14:02

Amit K. Saha