gpt4 book ai didi

android - 如何使用 OnTouch() 检查图像是否被触摸

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

我正在尝试获取 Touch of Image 上的 x,y 坐标,并且我想在该坐标上执行一些操作。那么,谁能告诉我如何在触摸图像时获取图像的 x、y 坐标。提前致谢。

我的代码-

public class MovableObject extends ImageView implements OnTouchListener{

Bitmap myBmp;
Paint myPaint = new Paint();
int MoveX = 0;

public MovableObject(Context context,int moveObject,Bitmap myBmp) {
super(context);
super.setClickable(true);
this.myBmp = myBmp;
myPaint.setColor(Color.WHITE);
this.MoveX = moveObject;
setOnTouchListener(this);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(myBmp, MoveX, 100, myPaint);
}
@Override
public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction() & MotionEvent.ACTION_MASK) {

case MotionEvent.ACTION_DOWN:
System.out.println("down...."+event.getX()+" "+event.getY());
case MotionEvent.ACTION_MOVE:
}
return true;
}
}

通过这个我得到了我点击的 x,y 坐标,但我想在我点击我的图片时得到 x,y 坐标。

最佳答案

MoveX - It is the starting x co-ordinate of your view.
MoveY -It is the starting y co-ordinate of your view.
imgHeiht - It is theHeight of the Image;
imgWidth - It is the Width of the Image;

event.getX()- It is the actual position of your X co-ordinate.

event.getY()- It is the actual position of your Y co-ordinate.

if(MoveX <= event.getX() && event.getX()<= (MoveX+imgWidth) && (MoveY <= event.getY() && (event.getY() <= MoveY+imgHeight))){
System.out.println("down...."+event.getX()+" "+event.getY());
}

这可能对你有帮助。

关于android - 如何使用 OnTouch() 检查图像是否被触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6938809/

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