如何获得我点击的位置的颜色?

ekr*_*emk 0 android android-imageview

我的屏幕上有图像视图。图像由灰色阴影组成。当我单击图片时,我想获取单击位置的rgb代码。在Android上可以吗?

dbe*_*m22 5

在您的onTouch通话中尝试以下操作:

public boolean onTouch (View v, MotionEvent ev) 
{

    final int action = ev.getAction();

    final int evX = (int) ev.getX();
    final int evY = (int) ev.getY();

    switch (action) {
        case MotionEvent.ACTION_DOWN :
           break;
        case MotionEvent.ACTION_UP :
           ImageView img = (ImageView) findViewById (YOUR_IMG_DRAWABLE);
           img.setDrawingCacheEnabled(true); 
           Bitmap imgbmp = Bitmap.createBitmap(img.getDrawingCache()); 
           img.setDrawingCacheEnabled(false);
           int pxl = imgbmp.getPixel(evX, evY);
           int redComponent = Color.red(pxl);
           int greenComponent = Color.green(pxl);
           int blueComponent = Color.blue(pxl);

           ...USE COLOR HERE...

           break;
    }

}
Run Code Online (Sandbox Code Playgroud)

希望有帮助!