【发布时间】:2013-11-25 10:27:17
【问题描述】:
我试图为布局内的任何对象获取 x,y,但是当对象位于屏幕的末端角落时我得到了这个值:
对象 X:266
对象 Y:361
屏幕宽度:320
屏幕高度:480
我怎样才能知道屏幕上的对象到底在哪里? (end,top,left,center)。
【问题讨论】:
标签: android dimensions
我试图为布局内的任何对象获取 x,y,但是当对象位于屏幕的末端角落时我得到了这个值:
对象 X:266
对象 Y:361
屏幕宽度:320
屏幕高度:480
我怎样才能知道屏幕上的对象到底在哪里? (end,top,left,center)。
【问题讨论】:
标签: android dimensions
希望这有帮助..
public Rect locateView(View view) {
Rect loc = new Rect();
int[] location = new int[2];
if (view == null) {
return loc;
}
view.getLocationOnScreen(location);
loc.left = location[0];
loc.top = location[1];
loc.right = loc.left + view.getWidth();
loc.bottom = loc.top + view.getHeight();
return loc;
}
我是这样使用方法的:
Rect r = TVGridUtils.locateView(activity.findViewById(R.id.imageview));
float touchX=r.left+((r.right-r.left)/2);
float touchY=(r.bottom);
注意:我必须得到我触摸的图像视图的中间点
【讨论】:
我认为您需要将对象的width 和height 添加到x 和y 值
要准确了解屏幕上的对象在哪里
X= 0,Y= 0 左上角
X= screenWidth-objectWidth,Y= 0 右上
X= 0,Y= screenHeight-objectHeight 左下角
X= screenWidth-objectWidth,Y= screenHeight-objectHeight 右下角
【讨论】:
Button b;
int x1 = b.getX();
int x2 = x1 + b.getWidth();
int y1 = b.getY();
int y2 = y1 + b.getHeight();
【讨论】: