如何确定用户是否点击了我的ImageView的某些区域?
设置(x,y)坐标边界以检测特定位图是否被点击时,我遇到了一些困难。
例如,如果位图的位置是(75,75),那么。即左上角就在这一点,然后如果用户在点(X,Y)触摸屏幕,那么简单的条件:
我有4个图像位置,我在画布上绘制(75,75),(645,75),(1215,75)和(75,490)。
它无法确定我点击它总是给我留言,“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;
}
谢谢你:)
答案 0 :(得分:0)
@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;
}
请你试试这部分代码而不是你的例子。