简体   繁体   中英

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. ie 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.getWidth())
                    && y >= 490 && y < (490 + mImageo.getHeight())) {
                Toast.makeText(getActivity(), "mImageo", Toast.LENGTH_SHORT).show();
                Log.e("TOUCHED", "X: " + x + " Y: " + y + mImageo.getWidth() + "-" + mImageo.getHeight());
            }

            return true;
    }
    return false;
}

Thankyou :)

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM