【问题标题】:Issue in getPixel() function, wrong colorgetPixel() 函数中的问题,颜色错误
【发布时间】: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


    【解决方案1】:

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

    过去我碰巧因为一些奇怪的原因,颜色不完全“适合”。

    【讨论】:

      猜你喜欢
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      • 2011-02-08
      • 1970-01-01
      • 1970-01-01
      • 2011-10-08
      相关资源
      最近更新 更多