2017-06-23 24 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中創建一個圖像併爲它着色,然後將背景設置爲圖像。

在過去,我發生了一些奇怪的原因,顏色並沒有完全「適合」。