Usman Farooq
Usman Farooq

Reputation: 161

Android: Detect touch event in circle shapes

Reference Image

enter image description here

In above image. I want to detect that in which circle user has taped his finger. If the user has touched black circle then a toast should be shown saying that "You are in Black circle". If it is blue then "You are in Blue Circle". And similarly for Red and Yellow Circles.

I know how to detect touch events DOWN, MOVE, PRESS etc and also x, y coordinates. But don't now how to detect circles area in that image.


EDIT

Actually the above image was just to make you understand my problem. In the actual scenario I don't want to detect the colour pixels. I want to get the exact area of each circle. Because in my case my background colour and my circle colours may be same. So detecting colour pixel will not solve my real problem.

Upvotes: 1

Views: 3446

Answers (3)

GLee
GLee

Reputation: 5093

If you know where the center of your circle is (and I assume you know the width of each ring), simply calculate the distance from the center in your on touch event as follows.

@Override public boolean onTouchEvent(MotionEvent ev) {
    double distanceFromCenter = Math.sqrt((CENTER_X - ev.getX())^2 + (CENTER_Y - ev.getY())^2);
    //Figure out which ring it's in. 
}  

You could also draw a bunch of overlapping circle shapes, but that would be more complex. See this to get started.

Upvotes: 1

Niko
Niko

Reputation: 8153

You get the pixel color of pressed position, then detect which color it hits closest and show Toast according to that

How to get pixel color in Android?

Upvotes: 0

Chintan Soni
Chintan Soni

Reputation: 25267

With this you will able to get the proprotion of Red, Green, Blue:

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

        int red = Color.red(pixel);
        int blue = Color.blue(pixel);
        int green = Color.green(pixel);

        Log.i("color", red+","+green+","+blue);
        return false;
    }
});

Upvotes: 0

Related Questions