James Montemagno
James Montemagno

Live, Love, Bike, and Code.

Tags


Twitter


Generating Android Color Palettes Super Fast with Rev 22.1

James MontemagnoJames Montemagno

Google went on a spree a few weeks back updating all of their support libraries to rev 22.1. If you haven’t read about all the changes be sure to read through the official Android Developer’s blog for a full breakdown. The most drastic change to the support libraries was to v7 AppCompat with the new AppCompatActivity, but there were several other enhancement to the libraries including one of my favorite Palette. If you don’t remember what palette is, it allows you to sample a color palette from any bitmap to theme your application. Here is a simple example of the before and after:

With rev 22.1, available as a NuGet package for your Xamarin.Android applications, the speed in which palettes are now generated are now 5 to 10 times faster! Here are some stats from Chris Banes’ blog:

After you update your NuGets to the latest version (currently 22.1.1.1 as of May 2nd 2015), you may notice that your old Palette methods were deprecated. This is because Google has replaced the older Palette.Generate and Palette.GenerateAsync methods with the tried and true Builder pattern. This will feel very familiar if you have worked with AlertDialogs or Notifications. It is basically a way of chaining property setters in a row (kind of linq style) since java doesn’t really have getters for properties that you could construct. So here is our old code:

In the latter method call the “this” is your current implementation of 

Palette.IPaletteAsyncListener, which will get a callback when the generate has been completed and the new way of generating a palette uses this same interface implementation, but looks a bit different:

Updating to the latest NuGet packages will have some drastic improvements in speed as you saw in Chris’ chart because according to Chris Banes: “In this release, we went back to some old-style optimizations for the colour quantization. Things like less object allocations, more appropriate data structures and a reduction in algorithmic complexity. “

There you have it! You can grab a sample from our Xamarin.Android samples gallery. Now go make things beautiful.

Live, Love, Bike, and Code

Comments