2017-06-23 21 views
1

我已经创建了一张表示汽车的图像,我用不同的颜色为汽车的每个组件着色,例如引擎盖的RGB颜色是251,252,252。getPixel()函数中的问题,错误的颜色

此图像通过TileView显示在屏幕上,我需要实现一个函数,该函数获取我触摸的像素的颜色,我张贴的功能,但返回给我一个不同的RGB颜色原来的一个。

我在这里示区别:

  • 原图:251252252
  • 位图图像:255,255,255

我不明白为什么使用位图的创建(而变色得到像素的颜色),或者问题出在getDrawingCache()函数中,可能会改变一些颜色值,真诚地我不知道...

Thi s是我的代码的一部分:

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; 
      } 
     }); 

回答

2

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

在过去,我发生了一些奇怪的原因,颜色并没有完全“适合”。