如何确定android中的比例因子

时间:2011-11-07 17:18:11

标签: android imageview zoom

如何在缩放后确定比例因子。我使用下面的代码进行捏缩放。缩放后,我必须在正确的位置绘制位图上的圆圈。我听说这是我们在变焦后用正确的点绘制的公式。

公式:

 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显示图像和缩放。 使用画布和位图我绘制圆圈并在绘制后设置到图像视图。

如何计算上述缩放变焦代码的比例因子?

提前致谢。

1 个答案:

答案 0 :(得分:0)

如果您使用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
}