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

为君一笑 提交于 2019-12-17 15:36:40

问题


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.


回答1:


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



回答2:


You can compute the image coordinates of the pixel that was clicked and read the pixel from the image data, like

Bitmap.getPixel(xcord,ycord)


来源:https://stackoverflow.com/questions/14920303/how-to-get-color-at-the-spotor-pixel-of-a-image-on-touch-event-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!