3

I've been having some problems implementing the new Palette library (on 4.4.4 with 'com.android.support:palette-v7:21.0.+'). I am trying to color a part of each item in a GridView which works fine but when I scroll an item off the screen then back on it changes to a wrong color for a few moments before it goes back to the right color.

I thought the issue might have been calling view.setBackgroundColor every time getView was called, so I made a check before my code if it had already had a color generated. This made it even worse. Each time I scrolled around colors would swap With enough scrolling all my colors have swapped places. It seems like the colors are switching with each other too, not random.

Heres a snippet of what my code looks like:

Palette.generateAsync(bitmap,
    new Palette.PaletteAsyncListener() {
        @Override
        public void onGenerated(Palette palette) {
            Palette.Swatch vibrant =
                    palette.getMutedSwatch();
            if (vibrant != null) {
                fView.findViewById(R.id.colored_bar).setBackgroundColor(
                        vibrant.getRgb());
            }
        }
    });

Does anyone know a way to work around this problem? I heard mention of caching the response from Palette but wasn't sure if that would mean doing any more than I already am. I also tried both synchronous and asynchronous uses of Palette. Thanks.

John Howard
  • 61,037
  • 23
  • 50
  • 66
  • Post the getView() method. – Nikola Despotoski Dec 19 '14 at 18:03
  • I have the same issue. I think the issue might be with convertView and Palette Async functions. By the time Palette is generated for a particular bitmap, the view to which the colour should be assigned is changing and hence the colour is set to that view. Did you resolve your issue, by any chance? – Vamsi Challa Jul 21 '15 at 10:21
  • Check out this post which might help you out. [Troubles with using Palette with GridView](http://stackoverflow.com/questions/31188934/troubles-with-using-palette-with-gridview) – Noitacol Jan 05 '16 at 03:29

1 Answers1

0

I found the solution to this. Basically the problem was that the palette object was created every time, which was both costly and inaccurate. Since the palette is static, I created a ViewHolder and stored the palette in that once it was created.

John Howard
  • 61,037
  • 23
  • 50
  • 66