简体   繁体   English

如何确定用户是否触摸了我的位图?

[英]How to determined if the user touch my bitmap?

How to determine if the user clicked in certain regions of my ImageView? 如何确定用户是否单击了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. 我在设置(x,y)坐标范围以检测何时单击或不单击特定位图时有些困难。

For example if the bitmap's position is (75,75),say. 例如,如果位图的位置是(75,75),请说。 ie top left corner is at this point, then if the user touches the screen at point (X,Y), then the simple conditional: 即,左上角在此点,那么,如果用户在(X,Y)点触摸屏幕,则简单的条件为:

I have 4 image position that i draw in canvas it's (75,75),(645,75),(1215,75) and (75,490). 我在画布上绘制的图像位置为4(75,75),(645,75),(1215,75)和(75,490)。

it wasn't able to determined what i click it always give me message, "mImaget" instead of the others. 它无法确定我单击的内容,它总是给我消息“ mImaget”,而不是其他消息。

@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. 您能否尝试一下代码的这一部分而不是您的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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