2016-07-25 104 views

回答

0

見這個例子..

使用OntouchListener檢測到您的ImageView的觸摸..

@Override 
    public boolean onTouch(View view, MotionEvent event) { 

     float eventX = event.getX(); 
     float eventY = event.getY(); 
     float[] eventXY = new float[]{eventX, eventY}; 

     int x = Integer.valueOf((int) eventXY[0]); 
     int y = Integer.valueOf((int) eventXY[1]); 

     Drawable imgDrawable = ((ImageView) view).getDrawable(); 
     Bitmap bitmap = ((BitmapDrawable) imgDrawable).getBitmap(); 

     //Limit x, y range within bitmap 
     if (x < 0) { 
      x = 0; 
     } else if (x > bitmap.getWidth() - 1) { 
      x = bitmap.getWidth() - 1; 
     } 

     if (y < 0) { 
      y = 0; 
     } else if (y > bitmap.getHeight() - 1) { 
      y = bitmap.getHeight() - 1; 
     } 

     int touchedRGB = bitmap.getPixel(x, y); 

     int r = Color.red(touchedRGB); 
     int b = Color.blue(touchedRGB); 
     int g = Color.green(touchedRGB); 

     String rr = Integer.toString(r); 
     String bb = Integer.toString(g); 
     String gg = Integer.toString(b); 

     String xyz = (rr+bb+gg); 

     colorRGB.setText("touched color: " + "#" + xyz); 

這裏的xyz是你的RGB顏色代碼..

+0

我想我可以使用這種方法(讀取圖片框架上的x + y,然後讀取圖片像素數組中的相同位置),但是我正在尋找使用react-native API的方法。所以它也將與iOS解決方案兼容。反正我會嘗試。 – jsdario