Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the dominant color of an image in an Android @drawable

You can understand why I'm trying to find the dominant color in an image if you use Windows 7. When your mouse over a program in the taskbar, the background of that particular program changes based on the most dominant color in the icon. I have noticed this technique used in other programs as well, but can't remember them off the top of my head.

I can see this being helpful in a number of UI techniques that I'm using to develop an application, and I was wondering how finding the most common color would be achieved from an Android drawable resource.

like image 757
tyler Avatar asked Dec 12 '11 07:12

tyler


People also ask

What is Android's color?

Android green is a shade of chartreuse or Caribbean green, defined by Google as the color of the "Android robot" logo for the Android operating system.

How can I check the pixels of an image in Android?

getY())); ImageView imageView = ((ImageView)v); Bitmap bitmap = ((BitmapDrawable)imageView. getDrawable()). getBitmap(); int pixel = bitmap. getPixel(x,y); int redValue = Color.


1 Answers

In Android 5.0 Lollipop, a class was added to help extract useful colors from a Bitmap. The Palette class, found in android.support.v7.graphics, can extract the following colors:

  • Vibrant
  • Vibrant Dark
  • Vibrant Light
  • Muted
  • Muted Dark
  • Muted Light

This Android training page gives all the details you need to use the class (I tried it myself in Android Studio and it was very straightforward): http://developer.android.com/training/material/drawables.html#ColorExtract

To quote:

The Android Support Library r21 and above includes the Palette class, which lets you extract prominent colors from an image. To extract these colors, pass a Bitmap object to the Palette.generate() static method in the background thread where you load your images. If you can't use that thread, call the Palette.generateAsync() method and provide a listener instead.*

You can retrieve the prominent colors from the image using the getter methods in the Palette class, such as Palette.getVibrantColor.

To use the Palette class in your project, add the following Gradle dependency to your app's module:

dependencies {     ...     implementation 'com.android.support:palette-v7:21.0.+' } 

Or if you're using androidx:

implementation 'androidx.palette:palette:1.0.0' 

If you need to use generateAsync(), here's how:

Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {     public void onGenerated(Palette palette) {         // Do something with colors...     } }); 

EDIT: Since the question asks how to extract colors from a drawable resource, you'd first have to convert the drawable to a bitmap to use the technique I've described. Luckily, that is quite simple using BitmapFactory:

Bitmap icon = BitmapFactory.decodeResource(context.getResources(),                                        R.drawable.icon_resource);` 
like image 80
Tony Wickham Avatar answered Sep 17 '22 12:09

Tony Wickham