android 放大后的坐标

发布于 2024-12-11 23:21:41 字数 2546 浏览 0 评论 0原文

我试图在缩放后绘制圆圈,但它没有绘制在正确的位置。没有变焦它工作正常。变焦后还需要考虑什么因素吗?像比例因子之类的东西。 圆的半径也随之增大。 即使在缩放后我也需要正确绘制。

      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坐标?以及缩放后圆的半径。 任何帮助将不胜感激。

I am trying to draw circle after zoom and it's not drawn in correct place. With out zoom it is working fine. Do we need to consider any factors after zoom ? Some thing like scale factor .
Radius of the circle also increasing.
I need to draw correctly even after zoom also.

      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);

         }
     }

How to calculate x, y coordinates after zoom in android? and also radius of the circle after zoom.
Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没有 2024-12-18 23:21:41

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

It's not clear to me from your code exactly what you're doing. But you have complete control over how you draw the circle. You can either scale the radius of the circle based on the given zoom level and redraw the circle with the new radius or scale the matrix of the canvas based on the zoomlevel.

千寻… 2024-12-18 23:21:41

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

If you use the ScaleGestureDetector.SimpleOnScaleGestureListener class you can do something like this:

@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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文