The color of the icon is also changed.

Source: Internet
Author: User

The color of the icon is also changed.

In the previous article, I wrote the idea and implementation of imitating QQ's theme change. When I saw the cool dog music replacing the wallpaper, the icon color also changed. This is also equivalent to replacing a set of theme, which is quite interesting, now let's take a look at how it is implemented.


It can be seen from the picture that the color of the icon is the main color of the wallpaper (some wallpapers are not like this, and some are not the main color of the image, I don't know if it is to filter out some colors and give it a default color value. It's not like the color extracted from the Palette. Here we will extract the main color.) So the first step is, this is to obtain the main color of an image. In the previous blog, the implementation principle of android L Palette is described as follows: how to classify an image into different colors and display them in a histogram. Here, we only need to get the highest column from the color histogram. I added a method in the ColorHistogram class to extract the main color, if the column is near the highest black or white, filter it out and take the other column (mColors stores various colors of the image, mColorCounts is used to save the corresponding number of such colors or how high it is)

 int maxColorCount = 0;    public int getMainColor(){    for(int i=0;i<mColors.length;i++){    if(ColorUtils.shouldIgnoreColor(mColors[i])){                continue;            }    if(maxColorCount<mColorCounts[i]){    maxColorCount = mColorCounts[i];            maxColorIndex = i;            }    }return mColors[maxColorIndex];    }

If you get the color and then replace the color of the original image, then OK.

Private static Drawable getDrawable (Context context, int drawableID, int beColor) {Bitmap bm = BitmapFactory. decodeResource (context. getResources (), drawableID); int w = bm. getWidth (); int h = bm. getHeight (); int px [] = new int [w * h]; bm. getPixels (px, 0, w, 0, 0, w, h); for (int I = 0; I <px. length; I ++) {int r = Color. red (px [I]); int g = Color. green (px [I]); int B = Color. blue (px [I]); int a = Color. alpha (px [I]); // Log. d ("color rgba", "" + r + "," + g + "," + B + "," + ); if (r> 240 & g> 240 & B> 240) {// The icons that need to be replaced by cool dogs are all white and rgb are all 255, you only need to replace the value corresponding to rgb. Here, the transparency of the original // is very important, because rgb is 255, but its original a is not necessarily 255, to make the image look good. r = Color. red (beColor); g = Color. green (beColor); B = Color. blue (beColor); px [I] = Color. argb (a, r, g, B) ;}} bm = Bitmap. createBitmap (px, 0, w, w, h, Bitmap. config. ARGB_8888); return new BitmapDrawable (bm );}
Here are the two methods. Let's take a look at the results.


Download the demo. If you are interested, you can see that cool dog music has a fuzzy background, which also provides methods.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.