簡體   English   中英

Android翻譯與規模

[英]Android translation in relation to scale

我有兩種布局。 一個(A)在其內容上具有ScaleGestureDetector,以處理PinchZoom功能,而另一個(B)動態添加了稱為Markers的ImageView。 B以鏡像的對齊方式和大小完美地位於A的頂部。

當用戶放大A時,我希望B上的標記以固定在相對於A的點上的方式平移。例如,如果用戶將標記放在興趣點(POI)上在A上,並放大到另一個點,標記應保持固定在POI上。

目前,我對每個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));
    }

其中trans是用於對A的內容執行postScale的Matrix,而focusXfocusY僅用於防止轉換值的累積。 startPoint是在MotionEvent.ACTION_DOWN情況下定義的第一次接觸點。

我的問題是,當用戶進一步放大標記時,翻譯會逐漸變得“不鉸鏈”。 較大的縮放手勢也會導致標記從其指定點浮動。 為了清楚起見,它們更靠近縮放點並遠離POI。

我懷疑代碼段底部的翻譯量缺少某些內容,可能與布局的大小有關。

如果我是你,我將采用以下方法:

  1. 在運動開始時找到標記位置。
  2. 查找數學以正確識別標記在運動中任何位置的位置。
  3. 使用setXsetY將其直接設置為該點。

這樣可以避免可能出現的任何浮點不確定性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM