放大android后的坐标

时间:2011-10-26 18:18:31

标签: android bitmap imageview zoom

我正在尝试在缩放后绘制圆圈并且它没有在正确的位置绘制。没有缩放它工作正常。我们需要在缩放后考虑任何因素吗?像比例因素这样的东西。 圆的半径也在增加。 即使在缩放后我也需要正确绘制。

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

             if (isZoomRequired == false)  //draw circle when zoom is off.
             {
             x = (int) rawEvent.getX();
                 y = (int) rawEvent.getY();
                 r = SettingsActivity.brushsize;

                pcanvas.drawCircle(x, y, r, mPaint);
                layers[1] = new BitmapDrawable(bitmap);
                LayerDrawable layerDrawable = new LayerDrawable(layers);
            view.setImageDrawable(layerDrawable);

            }
            else    //zoom when zoom is on or button selected                      
            {
             // 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);
               scale = newDist / oldDist;
               matrix.postScale(scale, scale, mid.x, mid.y);
               }
             }
             break;
         }


         view.setImageMatrix(matrix);

         }
     }

如何在放大android后计算x,y坐标?以及缩放后圆的半径。 任何帮助,将不胜感激。

2 个答案:

答案 0 :(得分:0)

我的代码并不清楚你正在做什么。但是您可以完全控制绘制圆的方式。您可以根据给定的缩放级别缩放圆的半径,并使用新的半径重绘圆,或者根据缩放级别缩放画布的矩阵。

答案 1 :(得分: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
}