如何获得图像中最突出的颜色 Android

How to get most prominent color in image Android

我注意到 Whatsapp Android 有一个功能,其中 CollapsingToolbar 获取用户图像最突出的颜色并将其设置为工具栏颜色,我尝试遍历图像并获取最突出的颜色,但横向太慢了,whatsApp 眨眼就完成了,以前做过的人可以告诉我怎么做吗?最后,当 activity 刚刚打开时,我如何默认设置我的 CollapsingToolbar 折叠,我注意到默认情况下它会展开,但我希望它折叠然后如果用户愿意,他们可以展开它滚动它,提前致谢。

我不能代表 WhatsApp 使用的策略,但支持库确实提供 'com.android.support:palette-v7:23.1.1' 可以为您做到这一点。

Palette.generate(Bitmap toGetColorsFrom)
Palette.generate(Bitmap toGetColorsFrom, int maximumPaletteSize)
Palette.generateAsync(Bitmap toGetColorsFrom, PaletteAsyncListener listener)
Palette.generateAsync(Bitmap toGetColorsFrom, int maximumPaletteSize, PaletteAsyncListener listener)

异步方法需要 Palette.PaletteAsyncListener 才能通知您结果。

从这些方法之一收到 Palette 对象后,您可以像这样设置工具栏背景颜色:

Palette palette = Palette.generate(myBitmap);
Palette.Swatch swatch = palette.getVibrantSwatch();
toolbar.setBackgroundColor(swatch.getRgb());

请注意,getVibrantSwatch() 是获取一组颜色的一种方法。您还可以使用:

Palette.getVibrantSwatch()
Palette.getDarkVibrantSwatch()
Palette.getLightVibrantSwatch()
Palette.getMutedSwatch()
Palette.getDarkMutedSwatch()
Palette.getLightMutedSwatch()

颜色使用 Palette library

要将折叠工具栏设置为在 activity 打开时关闭,您可以在 activity.

中使用进行设置

AppBarLayout layout = (AppBarLayout) findViewById(R.id.appbarID); layout.setExpanded(false);//here appbarID is the id of your appbarlayout in your xml file containing collapsing toolbar

希望对您有所帮助。