[英]Android translation in relation to scale
I have two layouts. 我有两种布局。 One (A) has a ScaleGestureDetector active on it's contents to handle a PinchZoom functionality while the other (B) has dynamically added ImageViews known as Markers.
一个(A)在其内容上具有ScaleGestureDetector,以处理PinchZoom功能,而另一个(B)动态添加了称为Markers的ImageView。 B sits perfectly on top of A with mirrored alignments and sizes.
B以镜像的对齐方式和大小完美地位于A的顶部。
When a user zooms in on A, I want the Markers on B to translate in a manner that they remain fixed on the points they were on relative to A. For example, if a user places a Marker on a point of interest (POI) on A, and zooms into a different point, the Marker should remain pinned on the POI. 当用户放大A时,我希望B上的标记以固定在相对于A的点上的方式平移。例如,如果用户将标记放在兴趣点(POI)上在A上,并放大到另一个点,标记应保持固定在POI上。
Currently I'm using the below code for every ScaleGestureDetector instance/action/run: 目前,我对每个ScaleGestureDetector实例/操作/运行使用以下代码:
float[] values = new float[9];
trans.getValues(values);
float transx = values[Matrix.MTRANS_X];
float transy = values[Matrix.MTRANS_Y];
float scalex = values[Matrix.MSCALE_X];
float scaley = values[Matrix.MSCALE_Y];
float scale = (float) Math.sqrt(scalex * scalex + scaley * scaley);
focusX = transx - focusX;
focusY = transy - focusY;
transx = focusX;
transy = focusY;
float markerX = marker.getX();
float markerY = marker.getY();
if(markerX > startPoint.x) {
marker.setTranslationX((-transx/scale) + (-transx%scale));
}else if(markerX < startPoint.x) {
marker.setTranslationX((transx/scale) + (transx%scale));
}
if(markerY > startPoint.y) {
marker.setTranslationY((-transy/scale) + (-transy%scale));
}else if(markerY < startPoint.y) {
marker.setTranslationY((transy/scale) + (transy%scale));
}
Where trans
is the Matrix used to perform postScales on A's contents and focusX
and focusY
are merely used to prevent cumulative build-up of the translation values. 其中
trans
是用于对A的内容执行postScale的Matrix,而focusX
和focusY
仅用于防止转换值的累积。 startPoint
is the point of first contact, defined in the MotionEvent.ACTION_DOWN case. startPoint
是在MotionEvent.ACTION_DOWN情况下定义的第一次接触点。
My problem is that the translation becomes progressively "unhinged" when the users zooms in further away from the markers. 我的问题是,当用户进一步放大标记时,翻译会逐渐变得“不铰链”。 Larger zoom gestures also cause the Markers to float away from their designated points.
较大的缩放手势也会导致标记从其指定点浮动。 For clarity, they get closer to the zoom point and move away from the POI.
为了清楚起见,它们更靠近缩放点并远离POI。
I suspect the translation amount at the bottom of the code segment is missing something, likely relative to the size of the layouts. 我怀疑代码段底部的翻译量缺少某些内容,可能与布局的大小有关。
If I were you, I would take the following approach: 如果我是你,我将采用以下方法:
setX
and setY
to set it directly to that point. setX
和setY
将其直接设置为该点。 That will avoid any floating point uncertainties that might arise. 这样可以避免可能出现的任何浮点不确定性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.