当手机尺寸改变时,视图的x和y坐标会发生变化,因此视图位于不同的位置

时间:2017-01-05 07:36:48

标签: android

我通过将视图转换为dp然后存储它来获取视图的坐标。当视图放在另一个密度手机的相同坐标时,它会出现在不同的位置 这是获取textview的x和y位置的代码。

int e1x = getdps((int)view.getX());
int e1y = getdps((int)view.getY());

int getdps(int dp) {
    float metrics = getApplicationContext().getResources().getDisplayMetrics().density;
    return (int) (metrics * dp+0.5f);
}

我被困在这里。我希望视图在所有Android手机上显示在同一位置。

1 个答案:

答案 0 :(得分:0)

屏幕上的X为imageView.getX()

图像的真实x越过:

ratioOfWidth=imageView.getWidth()/imageView.getDrawable().getIntrinsicWidth()

real x = image x * ratioOfWidth;

real y的工作相同。

Paint app核对是否与代码x相同 在Paint应用程序中,您可以在移动鼠标时看到x和y的底部状态。

第二个解决方案:

按油漆计算您的设计比率

例如我的宽度为100px,视图从50px开始

所以视图的宽度为.5

然后使用LinearLayout或PercentRelativeLayout或更好的ConstraintLayout

在约束布局中我将放置水平偏差.5