为什么的ImageView的getX,的getY,setX的和SETY不重present在相对布局的实际价值? [英] Why ImageView's getX, getY, setX and setY do not represent the actual value in the Relative Layout?

查看:243
本文介绍了为什么的ImageView的getX,的getY,setX的和SETY不重present在相对布局的实际价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个简单的应用程序只是为了更好地了解如何使用Android上的触摸事件。现在,我的活动只是移动的ImageView的屏幕(该MotionEvent坐标),在触摸的坐标。我可以管理运用一个简单的技巧来设置图像是由触摸值的新位置做这个坐标相对于图像(位置的getX() getY()以)。在 DX DY 的变量负责保存这种相对值,因此触摸保持图像内恒定的。

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.

现在我想把2 ImageViews的活动,并有只有两个固定点的ImageViews一旦用户比其他的ImageView中心拖1 ImageView的,两个图像切换的地方,但要做到这一点正确,我想获得图像中心的实际坐标。我评论的code,使其正常工作,以显示该问题的部分。正如你可以在第一张图片在创建视图时看到,图像坐标只是(0,0),无论它在哪里(只要初始位置定义为像一个布局参数 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( )返回的坐标,如果图像的左上角是当图像位于活动窗口的左上角显示屏的左上角。

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的()塞蒂()不要再present在屏幕上查看的位置,但我想通了,如何让屏幕上的视图对象的实际位置: getLocationOnScreen将(坐标)。其中,坐标是一个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的左上角图片我只是需要做的是这样的:

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);

然后,我其实可以比较这些坐标与触摸事件的 getRawX 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不重present在相对布局的实际价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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