2011-11-07 67 views
1

如何確定縮放後的比例因子。我正在使用下面的代碼捏縮放。縮放後,我必須在位圖上的正確位置繪製圓。我聽到了這個公式,我們必須用它來縮放後在正確的點繪製。如何在android中確定比例因子

公式:

float x = ev.getX()/mScaleFactor + rect.left; 
float y = ev.getY()/mScaleFactor + rect.top; 

我不知道如何計算mScaleFactor。

我使用下面的雙指縮放代碼,

public boolean onTouch(View v, MotionEvent rawEvent) { 
    WrapMotionEvent event = WrapMotionEvent.wrap(rawEvent); 
    // ... 
    ImageView view = (ImageView) v; 

    // Dump touch event to log 
    dumpEvent(event); 

    // Handle touch events here... 
    switch (event.getAction() & MotionEvent.ACTION_MASK) { 
    case MotionEvent.ACTION_DOWN: 
    savedMatrix.set(matrix); 
    start.set(event.getX(), event.getY()); 
    Log.d(TAG, "mode=DRAG"); 
    mode = DRAG; 
    break; 
    case MotionEvent.ACTION_POINTER_DOWN: 
    oldDist = spacing(event); 
    Log.d(TAG, "oldDist=" + oldDist); 
    if (oldDist > 10f) { 
     savedMatrix.set(matrix); 
     midPoint(mid, event); 
     mode = ZOOM; 
     Log.d(TAG, "mode=ZOOM"); 
    } 
    break; 
    case MotionEvent.ACTION_UP: 
    case MotionEvent.ACTION_POINTER_UP: 
    mode = NONE; 
    Log.d(TAG, "mode=NONE"); 
    break; 
    case MotionEvent.ACTION_MOVE: 
    if (mode == DRAG) { 
     // ... 
     matrix.set(savedMatrix); 
     matrix.postTranslate(event.getX() - start.x, 
       event.getY() - start.y); 
    } 
    else if (mode == ZOOM) { 
     float newDist = spacing(event); 
     Log.d(TAG, "newDist=" + newDist); 
     if (newDist > 10f) { 
      matrix.set(savedMatrix); 
      float scale = newDist/oldDist; 
      matrix.postScale(scale, scale, mid.x, mid.y); 
     } 
    } 
    break; 
    } 

    view.setImageMatrix(matrix); 
    return true; // indicate event was handled 

}

我使用的ImageView爲顯示圖像和縮放。 使用畫布和位圖我正在繪製圓形並將其設置爲繪製後的圖像視圖。

如何計算上述縮放縮放代碼的縮放係數?

在此先感謝。

回答

0

F你使用ScaleGestureDetector.SimpleOnScaleGestureListener類,你可以做這樣的事情:

@Override 
public boolean onScaleBegin(ScaleGestureDetector detector){ 
    //the focal point before taking into account the zoom/translations/etc 
    float startX = detector.getFocusX(); 
    float startY = detector.getFocusY(); 

    initialPoints = new float[]{startX, startY}; 
    if(transformMatrix.invert(inverseTransformMatrix)) 
     inverseTransformMatrix.mapPoints(initialPoints); 
    //initialPoints now contain the correct points based on the current zoom 
}