//调用
asBitmap(图片url);
//*************************************************
private void asBitmap(String path) {
Glide.with(getActivity()).asBitmap()
.load(path)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap>
transition) {
new Thread(new Runnable() {
@Override
public void run() {
getBigColor(bitmap);
}
}).run();
}
});
}
public void getBigColor(Bitmap bitmap) {
ArrayList<Integer> picturePixel = getPicturePixel(small(bitmap));
//计数相同颜色数量并保存
HashMap<Integer, Integer> color2 = new HashMap<>();
for (Integer color : picturePixel) {
if (color2.containsKey(color)) {
Integer integer = color2.get(color);
integer++;
color2.remove(color);
color2.put(color, integer);
} else {
color2.put(color, 1);
}
}
//挑选数量最多的颜色
Iterator iter = color2.entrySet().iterator();
int count = 0;
int color = 0;
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
int value = (int) entry.getValue();
if (count < value) {
count = value;
color = (int) entry.getKey();
android.os.Message message = new android.os.Message();
message.obj = color;
handler.sendMessage(message);
}
}
// bitmap.recycle();
bitmap = null;
}
public ArrayList<Integer> getPicturePixel(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// 保存所有的像素的数组,图片宽×高
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
// 需要分析
ArrayList<Integer> rgb = new ArrayList<>();
rgb.clear();
for (int i = 0; i < pixels.length; i++) {
int clr = pixels[i];
int red = (clr & 0x00ff0000) >> 16; // 取高两位
int green = (clr & 0x0000ff00) >> 8; // 取中两位
int blue = clr & 0x000000ff; // 取低两位
int color = Color.rgb(red, green, blue);
//除去白色和黑色
if (color != Color.WHITE && color != Color.BLACK) {
rgb.add(color);
}
}
// bitmap.recycle();
bitmap = null;
return rgb;
}
代码都是从别人博客复制整理的,如有侵权,联系立马删除!