如何在Android中的RGB轮内查找特定颜色的X,Y坐标



我在安卓活动上有一个 RGB 轮子 PNG 图像和类似#6DFFE0的颜色。

我想在RGB轮中找到颜色的X,Y坐标(位置(,以便我可以在Android中动态地将指示器移动到那里。 代码应该只在Android/Java中。

您可以循环所有像素并获取与您给定的颜色匹配的像素

ArrayList<String> pixels_matching_color = new ArrayList<>();
int color_to_find = Color.RED; //#FF0000 
ImageView imageView = new ImageView(this);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int total_width = bitmap.getWidth();
int total_height = bitmap.getHeight();
for (int y = 0; y < total_height; y++) {
for (int x = 0; x < total_width; x++) {
int pixel = bitmap.getPixel(x,y);
//Reading colors
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
//finally creating the color for pixel
int pixel_color = Color.rgb(redValue, blueValue, greenValue);
if (pixel_color == color_to_find){
pixels_matching_color.add(String.format("%s,%s",x,y));
}
}
}
//the array will contans the pixels that are matching the color you given
System.out.println(pixels_matching_color);

最新更新