Palette library changing colors while scrolling in GridView

独自空忆成欢 提交于 2019-12-12 14:41:39

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/27498142/palette-library-changing-colors-while-scrolling-in-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!