在Android 5.0 Lollipop中,添加了一个类来帮助从Bitmap中提取有用的颜色。在android.support.v7.graphics中找到的
Palette类可以提取以下颜色:
充满活力
充满活力的黑暗
充满活力的光
>静音
静音黑暗
静音灯
去引用:
The Android Support Library r21 and above includes the 07000
class, which lets you extract prominent colors from an image. To
extract these colors, pass a Bitmap object to the Palette.generate()
static method in the background thread where you load your images. If
you can’t use that thread, call the Palette.generateAsync() method and
provide a listener instead.*
You can retrieve the prominent colors from the image using the getter
methods in the Palette class, such as Palette.getVibrantColor.
To use the Palette class in your project, add the following Gradle
dependency to your app’s module:
06000
*如果您需要使用generateAsync(),请按以下步骤操作:
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
// Do something with colors...
}
});
编辑:
由于该问题询问如何从可绘制的资源中提取颜色,因此您首先必须将drawable转换为位图以使用我所描述的技术。幸运的是,使用BitmapFactory非常简单:
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_resource);`