我的应用程序是一个带有缩放和拖动选项的修改过的ImageViewer。
在包含这个修改后的ImageViewer的RelativeLayout中,我想要使用它作为AbsoluteLayout。
然后,可以向布局中添加新元素,以将它们定位在图像的某些点上。元素在数据库中的位置(x,y)以像素为单位,所以我猜只能通过编程方式添加它们,而不是使用XML。
然后,每次执行拖动或缩放操作时,这些元素的位置都会更新。
问题在于,图像的像素与RelativeLayout中的像素不匹配!因此,项目“更多或更少”地被放置在错误的位置上(距离(0,0)越远,误差就越大)。
我已经尝试过的事情:
Try different conversions because maybe the problem is related to the difference of densities between the Bitmap and the Layout:
Resources r = getResources(); float x = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics());
Try to use different methods in the layout to set the margins:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.leftMargin = (int) newPosition.x; params.topMargin = (int) newPosition.y; element.setLayoutParams(params);
I also wanted to use the options available for the RelativeLayout in XML but I really cant find it programmatically but I can find the way to specify the dimension, I mean:
android:layout_marginLeft ="50px" // or "50mm", or dp, or inches...