将视图的背景更改为与专辑封面颜色相匹配。

3

你好,我正在开发一款Android音乐播放器,希望能得到关于如何将背景色设置成当前播放歌曲的专辑封面颜色(就像Sony Walkman一样)的帮助。请问有人可以告诉我该如何实现吗?或者至少指导我应该如何开始。

我刚接触Android开发,如果有不妥之处请见谅,同时也抱歉我的英语不太好。


你将如何决定选择哪个像素作为图像的背景颜色? - Sagar Pilkhwal
这些链接希望能对你有所帮助:https://dev59.com/lWsz5IYBdhLWcg3wj4vahttps://dev59.com/iW025IYBdhLWcg3wAg7p - Awais
谢谢您的快速回复。我希望选择图像的主色作为背景颜色。感谢 Awais 提供的链接,我会查看它们。 - ray chamoun
1个回答

3
您可以使用v7调色板支持库。它包括Palette类,可让您从图像中提取突出的颜色。

https://developer.android.com/reference/android/support/v7/graphics/Palette.html

示例

enter image description here

build.gradle

compile 'com.android.support:palette-v7:23.4.0'

活动或碎片

public void updatePlayerBar(Bitmap bitmap) {
    Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
        public void onGenerated(Palette palette) {
            Palette.Swatch swatch = palette.getVibrantSwatch();
            if (swatch == null) swatch = palette.getMutedSwatch(); // Sometimes vibrant swatch is not available
            if (swatch != null) {
                // Set the background color of the player bar based on the swatch color
                mContent.setBackgroundColor(swatch.getRgb());

                // Update the track's title with the proper title text color
                mTitle.setTextColor(swatch.getTitleTextColor());

                // Update the artist name with the proper body text color
                mArtist.setTextColor(swatch.getBodyTextColor());
            }
        }
    });
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接