getPixel()函数问题,颜色错误

时间:2017-06-23 10:58:51

标签: java android colors bitmap pixel

我创建了一个代表汽车的图像,我用不同的颜色为汽车的每个部件着色,例如引擎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;
            }
        });

1 个答案:

答案 0 :(得分:2)

这可能是与图像有关的问题而不是代码。我建议您在png中创建一个图像并为其着色,然后将背景设置为图像。

在过去,我发生了一些奇怪的原因,颜色不适合"完整的。