Aiiz Cabuhat Ü February 2016

How to determined if the user touch my bitmap?

How to determine if the user clicked in certain regions of my ImageView?

I'm having a little bit of difficulty setting the (x,y) coordinate bounds to detect when a particular bitmap is being clicked on or not.

For example if the bitmap's position is (75,75),say. i.e. top left corner is at this point, then if the user touches the screen at point (X,Y), then the simple conditional:

I have 4 image position that i draw in canvas it's (75,75),(645,75),(1215,75) and (75,490).

it wasn't able to determined what i click it always give me message, "mImaget" instead of the others.

@Override
public boolean onTouch(View v, MotionEvent event) {
    int x = (int) event.getX();
    int y = (int) event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

             if (x >= 75 && x < (75 + mImaget.getWidth())
                    && y >= 75 && y < (75 + mImaget.getHeight())) {
                Toast.makeText(getActivity(), "mImaget", Toast.LENGTH_SHORT).show();
                Log.e("TOUCHED", "X: " + x + " Y: " + y + mImaget.getWidth() + "-" + mImaget.getHeight());
            }

            if (x >= 645 && x < (645 + mImageth.getWidth())
                    && y >= 75 && y < (75 + mImageth.getHeight())) {
                Toast.makeText(getActivity(), "mImageth", Toast.LENGTH_SHORT).show();
                Log.e("TOUCHED", "X: " + x + " Y: " + y + mImageth.getWidth() + "-" + mImageth.getHeight());
            }

            if (x >= 1215 && x < (1215 + mImagef.getWidth())
                    && y >= 75 && y < (75 + mImagef.getHeight())) {
                Toast.makeText(getActivity(), "mImagef", Toast.LENGTH_SHORT).show();
                Log.e("TOUCHED", "X: " + x + " Y: " + y + mImagef.getWidth() + "-" + mImagef.getHeight());
            }

            if (x >= 75 && x < (75 + mImageo        

Answers


Andrii Khrystovyi February 2016

@Override
public boolean onTouch(View v, MotionEvent event) {
    int x = (int) event.getX();
    int y = (int) event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            if (x >= 75 && x < (75 + mBack.getWidth())
                    && y >= 75 && y < (75 + mBack.getHeight())) {
                Toast.makeText(getActivity(), "Zone1: " + x +"-" + y, Toast.LENGTH_SHORT).show();
            }

            if (x >= 645 && x < (645 + mBack.getWidth())
                    && y >= 75 && y < (75 + mBack.getHeight())) {
                Toast.makeText(getActivity(), "Zone2: " + x +"-" + y, Toast.LENGTH_SHORT).show();
            }

            if (x >= 1215 && x < (1215 + mBack.getWidth())
                    && y >= 75 && y < (75 + mBack.getHeight())) {
                Toast.makeText(getActivity(), "Zone3: " + x +"-" + y, Toast.LENGTH_SHORT).show();
            }

            if (x >= 75 && x < (75 + mBack.getWidth())
                    && y >= 490 && y < (490 + mBack.getHeight())) {
                Toast.makeText(getActivity(), "Zone4: " + x +"-" + y, Toast.LENGTH_SHORT).show();
            }

            Log.d("TOUCHED", "X: " + x + " Y: " + y + mImaget.getWidth() + "-" + mImaget.getHeight());


            return true;
    }
    return false;
}

Could you please try this one part of code instead your example.

Post Status

Asked in February 2016
Viewed 3,823 times
Voted 10
Answered 1 times

Search




Leave an answer