Android中如何获取RGB色轮内指定颜色的X、Y坐标(Java实现)
Got it, here's a straightforward, practical approach to locate your target color in the color wheel PNG and move an indicator to that position using Android/Java code:
Step 1: Convert the Color Wheel Image to a Bitmap
First, you need a Bitmap instance of your color wheel to access its pixel data. You can get this either from an existing ImageView or directly from your drawable resources:
From an ImageView:
ImageView colorWheelIv = findViewById(R.id.color_wheel_image); colorWheelIv.setDrawingCacheEnabled(true); Bitmap colorWheelBitmap = Bitmap.createBitmap(colorWheelIv.getDrawingCache()); colorWheelIv.setDrawingCacheEnabled(false); // Disable cache once we have the bitmap
From Drawable Resources:
Bitmap colorWheelBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rgb_color_wheel);
Step 2: Scan the Bitmap for Your Target Color
Now iterate through each pixel to find matches with your target color (#6DFFE0 here). Android uses ARGB color values, so we’ll handle alpha transparency (if needed) and either look for an exact match or use a tolerance for anti-aliased images.
Exact Color Match (For Non-Anti-Aliased Wheels):
int targetColor = Color.parseColor("#6DFFE0"); int width = colorWheelBitmap.getWidth(); int height = colorWheelBitmap.getHeight(); Point targetPoint = null; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int pixelColor = colorWheelBitmap.getPixel(x, y); // Strip alpha channel if your image is opaque int pixelColorNoAlpha = pixelColor & 0xFFFFFF; int targetColorNoAlpha = targetColor & 0xFFFFFF; if (pixelColorNoAlpha == targetColorNoAlpha) { targetPoint = new Point(x, y); break; // Stop at first match, or remove to find all instances } } if (targetPoint != null) break; }
Color Match with Tolerance (For Anti-Aliased Wheels):
Most color wheels use anti-aliasing, so exact matches are rare. Use a tolerance threshold to find similar colors:
int targetColor = Color.parseColor("#6DFFE0"); int targetR = Color.red(targetColor); int targetG = Color.green(targetColor); int targetB = Color.blue(targetColor); int tolerance = 10; // Adjust this value based on your image's anti-aliasing Point targetPoint = null; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int pixelColor = colorWheelBitmap.getPixel(x, y); int pixelR = Color.red(pixelColor); int pixelG = Color.green(pixelColor); int pixelB = Color.blue(pixelColor); // Check if RGB values fall within the tolerance range if (Math.abs(pixelR - targetR) <= tolerance && Math.abs(pixelG - targetG) <= tolerance && Math.abs(pixelB - targetB) <= tolerance) { targetPoint = new Point(x, y); break; } } if (targetPoint != null) break; }
Step 3: Move the Indicator to the Found Coordinates
Once you have the target X/Y coordinates, adjust them to center your indicator (like an ImageView) over the color, then move it:
ImageView indicatorIv = findViewById(R.id.color_indicator); if (targetPoint != null) { // Offset coordinates to center the indicator over the color int indicatorX = targetPoint.x - (indicatorIv.getWidth() / 2); int indicatorY = targetPoint.y - (indicatorIv.getHeight() / 2); // Move the indicator indicatorIv.setX(indicatorX); indicatorIv.setY(indicatorY); } else { // Handle case where the color wasn't found Toast.makeText(this, "Target color not found in color wheel", Toast.LENGTH_SHORT).show(); }
Key Tips for Smooth Operation
- Memory Management: Recycle the bitmap when you’re done to avoid memory leaks:
if (!colorWheelBitmap.isRecycled()) { colorWheelBitmap.recycle(); } - Multiple Matches: If your color wheel has multiple instances of the target color, calculate the distance from the wheel’s center for each match and pick the closest one.
- Performance: For large images, scan only the circular area of the color wheel instead of the entire rectangle to speed up the process.
内容的提问来源于stack exchange,提问作者Dinesh Raturi

