本文介绍了获取相对于视图的触摸坐标(ScreenToClient等效?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我在Android应用程序中向用户显示的图像。



我希望能够分辨出他们触摸图像的位置。



我可以通过实现OnTouchListener轻松获取屏幕坐标

  private OnTouchListener image_Listener = new OnTouchListener(){
@Override
public boolean onTouch(View v,MotionEvent event){
if(event.getAction()== MotionEvent.ACTION_UP){
float x = event.getX();
float y = event.getY();
返回true;
}
返回false;
}
};

但是,这些是绝对坐标。我真正需要的是将其转换为相对于View的坐标的函数..



我做了一些挖掘,但我似乎无法出现除了我自己试图实现ScreenToClient方法之外的任何事情..



有没有人知道这个的好方法,或者我只是要自己动手?



这似乎是一种可用的方法 - 这就是我要问的原因。



谢谢大家的时间。



编辑:我不太确定坐标是否已经相对于视图。我本来可以发誓说坐标是绝对的,但是经过一些修修补补 - 我的工作正如我所期望的那样......对不起那些吵闹的家伙。

解决方案

回答一下,触摸与某种方式相关。是的,它们是触摸的原始屏幕坐标。但是,由于您将触摸事件注册到视图本身,因此只有在用户实际触摸视图时才会触发。这意味着,即使屏幕坐标是原始屏幕坐标,它们也在视图本身的范围内。



如果你想得到x / y的坐标仅触及与自身相关的视图,然后您可以使用屏幕坐标和y坐标的顶部坐标减去左坐标,如下所示:

  private OnTouchListener image_Listener = new OnTouchListener(){
@Override
public boolean onTouch(View v,MotionEvent event){
if(event.getAction()== MotionEvent.ACTION_UP ){
float screenX = event.getX();
float screenY = event.getY();
float viewX = screenX - v.getLeft();
float viewY = screenY - v.getTop();
返回true;
}
返回false;
}
};

这样,如果用户在屏幕上触摸了40 x像素,但左边距已设置到10,然后 viewX 将是30.如果你将视图向下移动40 x像素并且用户触摸了视图中的相同位置,它仍然是30。 / p>

如果你在视图中包含填充作为填充仍然算作视图,即使它没有显示任何内容,它会稍微复杂一点。



编辑:



我应该指出皮埃尔说的是真的。如果您触摸视图,则即使您将手指拖到视图边界之外,它也会继续接收触摸事件,直到手指抬起为止。这意味着,ACTION_UP可以接收视图中不在范围内的触摸坐标。


I have an image I am displaying to a user in my android application.

I want to be able to tell where they 'touch' the image.

I can easily get the screen coordinates by implementing an OnTouchListener

 private OnTouchListener image_Listener = new OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP) {
            float x = event.getX();
            float y = event.getY();
            return true;
        }
        return false;
    }
};

However, these are absolute coordinates. What I really need is a function to convert this into coordinates relative to a View..

I have done a bit of digging, but I can't seem to turn up anything, aside from me trying to implement a ScreenToClient method myself..

Does anyone know of a good solution to this, or am I just going to have to roll my own?

It seems like the kind of method that would be available - that's why I'm asking.

Thank you all for your time.

EDIT: I'm not so sure that the coordinates aren't already relative to the view. I could have swore reading that coordinates were absolute, but after some tinkering - I have things working as I expected.. Sorry for the noise guys.

解决方案

Just to answer, the touches are relative to view in a way. Yes, they are raw screen coordinates of the touch. However, since you register the touch event to the view itself, it will only fire if the user actually touches the view. That means, even though the screen coordinates are raw screen coordinates, they are within the bounds of the view itself.

If you want to get the x/y coordinates of the touched view in relation to only itself, then you can subract the left coordinate with the screen coordinate and the top coordinate from the y coordinate like so:

private OnTouchListener image_Listener = new OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP) {
            float screenX = event.getX();
            float screenY = event.getY();
            float viewX = screenX - v.getLeft();
            float viewY = screenY - v.getTop();
            return true;
        }
        return false;
    }
};

With that, if the user touched 40 x-pixels down the screen, but the left margin was set to 10, then viewX would be 30. If you moved the view down 40 x-pixels and the user touched the same spot in the view, it would still be 30.

It gets slightly more complicated if you included padding as padding within a View still counts as a View even though it doesn't display anything.

EDIT:

I should point out that what Pierre said is true. If you touch a view, then it will continue to receive touch events until the finger is lifted even if you drag your finger outside the view bounds. This means, ACTION_UP can receive touch coordinates for your view that are not within the bounds.

这篇关于获取相对于视图的触摸坐标(ScreenToClient等效?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:10