android 获取图片主色调

本文介绍如何在 Android 应用中使用 GlidePalette 库加载图片,并提取图片的主色调来设置 TextView 的背景颜色和文字颜色。通过 Glide 的扩展库 GlidePalette,可以轻松地为 ImageView 加载图片资源,并同时获取该图片的主要颜色,进而应用于 UI 设计中。

在build.gradle添加依赖

compile 'com.github.florent37:glidepalette:1.0.6'
使用

TextView tvColor=(TextView) findViewById(R.id.testte);
ImageView imageView = (ImageView)findViewById(R.id.testimage);
tvColor.setText(style);
ImageManager.loadAndPalette(MainActivity.this, "http://f.hiphotos.baidu.com/image/pic/item/503d269759ee3d6db032f61b48166d224e4ade6e.jpg", imageView, tvColor);
主方法

/*
    加载指定URL的图片并设置到targetView, 取主色调palete
 */
public static void loadAndPalette(Context context, String url, ImageView targetView, TextView textView) {
    Glide.with(context).load(url)
            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .listener(GlidePalette.with(url)
                    .use(GlidePalette.Profile.VIBRANT)
                    .intoBackground(textView, GlidePalette.Swatch.RGB)
                    .intoTextColor(textView, GlidePalette.Swatch.BODY_TEXT_COLOR)
                    .crossfade(true)
            )
            .into(targetView);
}

Android 中提取图片的主要颜色(Dominant Color)可以通过多种方式实现,包括使用系统提供的 API 或者自定义算法来计算。以下是几种常见的方法: ### 方法一:使用 `Palette` 库 Android 提供了 `Palette` 类,它可以从图片中提取出几种主要的颜色方案,例如主色调、高亮色等。 ```java import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v7.graphics.Palette; public void extractDominantColor(String imagePath) { Bitmap bitmap = BitmapFactory.decodeFile(imagePath); if (bitmap != null) { Palette.from(bitmap).generate(palette -> { // 获取主要颜色 int dominantColor = palette.getDominantColor(0x000000); // 默认值为黑色 System.out.println("Dominant color: " + Integer.toHexString(dominantColor)); }); } } ``` 此方法适用于大多数情况,并且可以轻松地集成到应用的 UI 设计中,比如用于设置主题背景色或文本颜色 [^4]。 ### 方法二:使用像素遍历与统计分析 如果希望不依赖第三方库或者需要更精细的控制,可以手动遍历图像的所有像素并进行统计分析以找出最常见的颜色组合。 ```java public int getDominantColor(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int[] pixels = new int[size]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); // 使用 HashMap 来统计每种颜色出现的次数 Map<Integer, Integer> colorCountMap = new HashMap<>(); for (int pixel : pixels) { int red = (pixel >> 16) & 0xFF; int green = (pixel >> 8) & 0xFF; int blue = pixel & 0xFF; // 忽略纯白色和纯黑色 if (red > 250 && green > 250 && blue > 250) continue; if (red < 5 && green < 5 && blue < 5) continue; int simplifiedColor = (red / 32) << 10 | (green / 32) << 5 | (blue / 32); // 简化颜色空间 colorCountMap.put(simplifiedColor, colorCountMap.getOrDefault(simplifiedColor, 0) + 1); } // 找出出现次数最多的颜色 return Collections.max(colorCountMap.entrySet(), Map.Entry.comparingByValue()).getKey(); } ``` 这种方法允许开发者对颜色采样过程有更多的控制权,比如可以忽略过于明亮或暗淡的颜色 [^1]。 ### 方法三:结合 HSV 色彩模型 为了更好地识别视觉上显著的颜色,可以将 RGB 值转换为 HSV 模型,并基于饱和度和其他因素调整权重。 ```python def get_dominant_color(image): max_score = 0.0001 dominant_color = None for count, (r, g, b) in image.getcolors(image.size[0]*image.size[1]): saturation = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)[1] y = min(abs(r*2104+g*4130+b*802+4096+131072)>>13,235) y = (y-16.0)/(235-16) if y > 0.9: continue score = (saturation+0.1)*count if score > max_score: max_score = score dominant_color = (r,g,b) return dominant_color ``` 这段 Python 代码展示了如何通过考虑饱和度和亮度来优化主导色的选择 [^1]。 ### 方法四:平均颜色法 对于某些应用场景,可以直接计算所有非透明像素的平均 RGB 值作为主要颜色。 ```java public int getAverageColor(Bitmap bitmap) { long r = 0, g = 0, b = 0; int width = bitmap.getWidth(); int height = bitmap.getHeight(); for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { int pixel = bitmap.getPixel(x, y); r += (pixel >> 16) & 0xFF; g += (pixel >> 8) & 0xFF; b += pixel & 0xFF; } } int totalPixels = width * height; return Color.rgb((int)(r / totalPixels), (int)(g / totalPixels), (int)(b / totalPixels)); } ``` 这种方法简单直接,但可能不如其他方法准确,因为它没有考虑到人眼对不同颜色敏感度的不同。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值