【发布时间】:2017-11-26 22:26:54
【问题描述】:
我创建了一个代表汽车的图像,我用不同的颜色为汽车的每个组件着色,例如引擎盖的 RGB 颜色是 251,252,252。
此图像通过 TileView 显示在屏幕上,我需要实现一个函数来获取我触摸的像素的颜色,我发布的函数有效,但返回给我的 RGB 颜色与原始颜色不同一个。
这里我展示了不同之处:
- 原图:251,252,252
- 位图图像:255,255,255
我不明白为什么在创建位图(用于获取像素的颜色)时颜色会发生变化,或者问题出在 getDrawingCache() 函数中,可能会改变一些颜色值,我真的不明白不知道……
这是我的代码的一部分:
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;
}
});
【问题讨论】:
标签: java android colors bitmap pixel