Java getPixel()函数中存在问题,颜色错误
我已经创建了一个代表汽车的图像,我用不同的颜色给汽车的每个部件上色,例如引擎盖RGB颜色是251252 这幅图像通过TileView显示在屏幕上,我需要实现一个功能,获取我接触的像素的颜色,我发布的功能可以工作,但返回给我的RGB颜色与原始颜色不同 在这里,我展示了不同之处:Java getPixel()函数中存在问题,颜色错误,java,android,colors,bitmap,pixel,Java,Android,Colors,Bitmap,Pixel,我已经创建了一个代表汽车的图像,我用不同的颜色给汽车的每个部件上色,例如引擎盖RGB颜色是251252 这幅图像通过TileView显示在屏幕上,我需要实现一个功能,获取我接触的像素的颜色,我发布的功能可以工作,但返回给我的RGB颜色与原始颜色不同 在这里,我展示了不同之处: 原图:25125252 位图图像:255255 我不明白为什么在创建位图(用于获取像素的颜色)时颜色会发生变化,或者可能是getDrawingCache()函数中的问题可能会改变一些颜色值,我真的不知道 这是我代码的一
- 原图:25125252
- 位图图像:255255
tileView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int eventAction = motionEvent.getAction();
switch (eventAction) {
case MotionEvent.ACTION_DOWN:
double x = tileView.getCoordinateTranslater().translateAndScaleAbsoluteToRelativeX(tileView.getScrollX() + motionEvent.getX(), tileView.getScale());
double y = tileView.getCoordinateTranslater().translateAndScaleAbsoluteToRelativeY(tileView.getScrollY() + motionEvent.getY(), tileView.getScale());
try {
tileView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(tileView.getDrawingCache());
File file = new File(Environment.getExternalStorageDirectory() + "/bitmap.png");
bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(file));
int touchColor;
if (bitmap == null) {
touchColor = 0;
} else {
touchColor = bitmap.getPixel((int) motionEvent.getX(), (int) motionEvent.getY());
}
int redValue = Color.red(touchColor);
int blueValue = Color.blue(touchColor);
int greenValue = Color.green(touchColor);
Log.wtf("DEBUG", "-Red: " + redValue + " -Green: " + greenValue + " -Blue: " + blueValue);
tileView.setDrawingCacheEnabled(false);
tileView.destroyDrawingCache();
bitmap.recycle();
} catch (Exception e) {
}
addPin(tileView, x, y);
break;
}
return false;
}
});
这可能是与图像相关的问题,而不是与代码相关的问题。我建议您用png创建一个图像并对其着色,然后将背景设置为图像 在过去,我碰巧因为一些奇怪的原因,这些颜色并不完全“适合”