我需要一些有关Android中视图坐标的帮助。
我有一个父视图(如RelativeLayout)和一个子视图(如ImageView),当我获取x和y值(即y值)的子视图后,用户将子视图动态设置为在x和y位置的父视图与用户设置的值不同。
这是我的代码,其中设置了chld视图,x,y,宽度,高度是动态值。
final ImageView imageView = new ImageView(context);
imageView.setImageBitmap(desiredImage);
int viewId = View.generateViewId();
imageView.setId( viewId );
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( width, height );
params.leftMargin = x;
params.topMargin = y;
parentView.addView(imageView, params);
这是使用不同方法打印x和y值的方法,所有方法都无法给出准确的值
public void printAttribs(int viewId){
Log.e( TAG, "printAttribs-" +viewId );
final View view = parentView.findViewById(viewId);
if (view != null) {
int location[] = new int[2];
view.getLocationInWindow(location);
Log.e(TAG,
"view getLeft" + view.getLeft()
+ ",getTop" + view.getTop()
+ ",getWidth" + view.getWidth()
+ ",getHeight" + view.getHeight()
+ ",getMeasuredWidth" + view.getMeasuredWidth()
+ ",getMeasuredHeight" + view.getMeasuredHeight()
+ ",getX" + view.getX()
+ ",getY" + view.getY()
+ ",getRelativeLeft" + getRelativeLeft(view)
+ ",getRelativeTop" + getRelativeTop(view)
+ ",location[0]" + location[0]
+ ",location[1]" + location[1]
);
} else {
Log.e(TAG, "printAttribs view NULL");
}
}
我希望有人能找到可行的解决方案,谢谢。