如何在 android 中以编程方式获取应用程序图标颜色代码?

How to get the app icon color code programmaticallyin android?

实际上我的要求是获取我设备中安装的任何应用程序的图标颜色。 我想显示那种颜色的锁屏。那么如何以编程方式获取任何图标的颜色代码?

如果你想从一个图标中获取所有颜色的RGB值--

Bitmap bitmap;
// create  the bitmap from your obtained image
int pixel = bitmap.getPixel(x,y); // x,y is the desired position of the target pixel, for full imag, you have to do the same thing in a loop

int red = Color.red(pixel);
int green = Color.green(pixel);
int blue = Color.blue(pixel);

返回的 int 值是您的标准 0 - 255。您可以修改此代码并从任何地方获取颜色,前提是您可以将其转换为位图。您可以使用 Color API 来获取实际的 RGB 值,如下所示:

int rgb = Color.rgb(red, blue, green); // rgb value of a single pixel, 

现在,为了一次获取所有像素,您可以使用 Bitmap.getPixels()

int[] allPixels = new int[bitmap.getWidth()*bitmap.getHeight()];
bitmap.getPixels(allPixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

我不知道你所说的获取图标颜色是什么意思,因为图标是图像,但你可以像这样获取已知应用程序的图标:

您可以像这样获取所有已安装的应用程序: How to get all apps installed on android phone

希望对您有所帮助。