gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:57:20 25 4
gpt4 key购买 nike

如何确定用户是否点击了我的 ImageView 的某些区域?

我在设置 (x,y) 坐标范围来​​检测特定位图是否被单击时遇到了一些困难。

例如,如果位图的位置是 (75,75)。即左上角位于此时,那么如果用户触摸屏幕的点(X,Y),则简单的条件:

我在 Canvas 上绘制了 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;
}

谢谢:)

最佳答案

@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;
}

您可以尝试用这部分代码来代替您的示例吗?

关于java - 如何确定用户是否触摸了我的位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35265754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com