在 Android @drawable 中查找图像的主色 [英] Finding the dominant color of an image in an Android @drawable

查看:33
本文介绍了在 Android @drawable 中查找图像的主色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您使用的是 Windows 7,您就可以理解为什么我要尝试在图像中找到主色.当您将鼠标悬停在任务栏中的某个程序上时,该特定程序的背景会根据图标.我也注意到其他程序中也使用了这种技术,但我一时想不起来.

You can understand why I'm trying to find the dominant color in an image if you use Windows 7. When your mouse over a program in the taskbar, the background of that particular program changes based on the most dominant color in the icon. I have noticed this technique used in other programs as well, but can't remember them off the top of my head.

我可以看到这对我用来开发应用程序的许多 UI 技术很有帮助,我想知道如何从 Android 可绘制资源中找到最常见的颜色.

I can see this being helpful in a number of UI techniques that I'm using to develop an application, and I was wondering how finding the most common color would be achieved from an Android drawable resource.

推荐答案

在 Android 5.0 Lollipop 中,添加了一个类来帮助从位图中提取有用的颜色.Palette 类,位于 android.support.v7.graphics,可以提取以下颜色:

In Android 5.0 Lollipop, a class was added to help extract useful colors from a Bitmap. The Palette class, found in android.support.v7.graphics, can extract the following colors:

  • 充满活力
  • 充满活力的黑暗
  • 充满活力的灯光
  • 静音
  • 柔和的黑暗
  • 静音

此 Android 培训页面提供了使用该课程所需的所有详细信息(我自己在 Android Studio 中尝试过,非常简单):http://developer.android.com/training/material/drawables.html#ColorExtract

This Android training page gives all the details you need to use the class (I tried it myself in Android Studio and it was very straightforward): http://developer.android.com/training/material/drawables.html#ColorExtract

引用:

Android 支持库 r21 及更高版本包括 Palette类,它可以让您从图像中提取突出的颜色.到提取这些颜色,将 Bitmap 对象传递给 Palette.generate()加载图像的后台线程中的静态方法.如果你不能使用那个线程,调用 Palette.generateAsync() 方法和提供一个监听器.*

The Android Support Library r21 and above includes the Palette 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.*

您可以使用 getter 从图像中检索突出的颜色Palette 类中的方法,例如 Palette.getVibrantColor.

You can retrieve the prominent colors from the image using the getter methods in the Palette class, such as Palette.getVibrantColor.

要在您的项目中使用 Palette 类,请添加以下 Gradle对应用模块的依赖:

To use the Palette class in your project, add the following Gradle dependency to your app's module:

dependencies {
    ...
    implementation 'com.android.support:palette-v7:21.0.+'
}

或者,如果您使用的是 androidx:

Or if you're using androidx:

implementation 'androidx.palette:palette:1.0.0'

如果您需要使用 generateAsync(),方法如下:

Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
    public void onGenerated(Palette palette) {
        // Do something with colors...
    }
});

由于问题询问如何从可绘制资源中提取颜色,因此您首先必须将可绘制资源转换为位图以使用我所描述的技术.幸运的是,使用 BitmapFactory 这很简单:

Since the question asks how to extract colors from a drawable resource, you'd first have to convert the drawable to a bitmap to use the technique I've described. Luckily, that is quite simple using BitmapFactory:

Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                                       R.drawable.icon_resource);`

这篇关于在 Android @drawable 中查找图像的主色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆