为什么ImageView的getX、getY、setX、setY在RelativeLayout中不代表实际值? [英] Why ImageView's getX, getY, setX and setY do not represent the actual value in the Relative Layout?

查看:25
本文介绍了为什么ImageView的getX、getY、setX、setY在RelativeLayout中不代表实际值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个简单的应用程序,只是为了更好地了解如何在 Android 上使用触控事件.现在,我的 Activity 只是将 ImageView 移动到屏幕上触摸的坐标(MotionEvent 的坐标).我可以通过应用一个简单的技巧将图像的新位置设置为由相对于图像位置的触摸坐标(getX()getY()).dxdy 变量负责存储这些相对值,以便触摸在图像中保持恒定.

I am trying to make a simple app just to understand better how to use Touch events on Android. Right now, my Activity just moves an ImageView to the coordinates of the touch on the screen (the MotionEvent's coordinates). I could manage to do this by applying a simple trick to set the new position of the image to a value that is made of the touch coordinates relative to the position of the image (getX() and getY()). The dx and dy variables are responsible to store this relative values so the touch keeps constant within the image.

现在我想在 Activity 中放置 2 个 ImageView,并且只有两个 ImageView 固定点,一旦用户将一个 ImageView 拖到另一个 ImageView 的中心,这两个图像就会切换位置,但要正确地做到这一点,我想得到图像中心的真实坐标.我评论了代码中使其正常工作以显示问题的部分.正如您在创建视图时的第一张图片中看到的那样,图像坐标只是(0, 0),无论它在哪里(只要初始position 定义为布局参数,如 CENTER_IN_PARENT).

Now I want to put 2 ImageViews in the Activity and have only two fixed spots for ImageViews and once the user drags one ImageView over the other ImageView's center, the two images switch places, but to do that correctly I would like to get the real coordinates of the image center. I commented the parts of the code that make it work correctly to show the issue. As you can see in the first picture when the view is created, the Image Coordinates are simply (0, 0), no matter where it is (as long as the initial position is defined as a layout parameter like CENTER_IN_PARENT).

正如您在 第二张图片 中看到的,据我观察 ImageView 的 getX()getY() 返回坐标为如果图像位于 ACTIVITY WINDOW 的左上角时,图像的左上角位于 DISPLAY 的左上角.

As you can see in the second picture and as far as I observed ImageView's getX() and getY() return the coordinate as if the top-left corner of the image was in the top-left corner of the DISPLAY when the image is located at the top-left corner of the ACTIVITY WINDOW.

我想了解为什么会发生这种情况,以及是否有人知道如何实际获取 ImageView 的坐标,该坐标可以直接与 MotionEvent 的坐标进行比较.

I would like to understand why that happens and if anyone knows how to actually get the coordinates of ImageView that can be directly compared to the coordinates of MotionEvent.

图片:

第一张图片

第二张图片

推荐答案

我还是不知道为什么getX(), getY(), setX()setY() 不代表 View 在屏幕上的位置,但是我想出了如何获取 View 对象在屏幕上的实际位置:<代码>getLocationOnScreen(坐标).其中 coordinates 是一个 2 位 int 数组 (int[2]).

I still don't know exactly why getX(), getY(), setX(), and setY() do not represent the position of the View on the screen, but I figured out how to get the actual location of the view objects on the screen: getLocationOnScreen(coordinates). Where coordinates is a 2 position int array (int[2]).

例如,要获得与我的 ImageView image 左上角对应的实际值,我只需要执行以下操作:

For example, to get the real value correspondent to the top-left corner of my ImageView image I simply need to do something like:

int[] img_coordinates = new int[2];
ImageView image = (ImageView) findViewById(R.id.myImage);

image.getLocationOnScreen(img_coordinates);

然后我实际上可以将这些坐标与触摸事件的 getRawXgetRawY 进行比较.

And then I can actually compare these coordinates with the Touch-event's getRawX and getRawY.

如果我想获得我的观点的中心点,我只需要这样做:

If I want to get the center point of my view I just need to do this:

x_center = (double)img_coordinates[0] + image.getWidth()/2.0;
y_center = (double)img_coordinates[1] + image.getHeight()/2.0;

我希望这对其他人也有帮助,我花了一段时间才找到这种方法,实际上我不确定这是否是最好的方法,但它对我有用.

I hope this helps other people too, it took me a while to find this approach and I am actually not sure if it is the best approach but it does the trick for me.

这篇关于为什么ImageView的getX、getY、setX、setY在RelativeLayout中不代表实际值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆