how to get color at the spot(or pixel) of a image on touch event in android

后端 未结 2 533
无人共我
无人共我 2020-12-05 03:48

I want to get the color of the spot or pixel where I will touch a image in Android. I searched a lot on net, but got nothing. Please anyone help me.

2条回答
  •  爱一瞬间的悲伤
    2020-12-05 04:12

    try this:

    final Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
    imageView.setOnTouchListener(new OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event){
            int x = (int)event.getX();
            int y = (int)event.getY();
            int pixel = bitmap.getPixel(x,y);
    
            //then do what you want with the pixel data, e.g
            int redValue = Color.red(pixel);
            int blueValue = Color.blue(pixel);
            int greenValue = Color.green(pixel);        
            return false;
            }
       });
    

提交回复
热议问题