Android 从缩放的画布获得精确的触摸位置(从屏幕坐标到画布坐标)

Android 从缩放的画布获得精确的触摸位置(从屏幕坐标到画布坐标),android,canvas,touch,scale,Android,Canvas,Touch,Scale,嗨 我有一个在屏幕范围内缩放/平移图像的应用程序。 我的CustomFrameLayout中有自定义ImageView类,定义如下 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" and

我有一个在屏幕范围内缩放/平移图像的应用程序。 我的CustomFrameLayout中有自定义ImageView类,定义如下

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:background="@android:color/darker_gray" >

<com.example.panzoomapplication.panzoom.CustomFrameLayout
                        android:id="@+id/custom_frame_layout"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        >

<com.example.panzoomapplication.panzoom.CustomImageView
                            android:id="@+id/drawing_view"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:drawingCacheQuality="high"
                            android:layout_gravity="center"
                            android:src="@drawable/image"
                            android:adjustViewBounds="true" />
</com.example.panzoomapplication.panzoom.CustomFrameLayout>

</FrameLayout>
当mScalFactor=1(或处于初始状态)时,画布可以很好地缩放并采取正确的触摸位置。但当按大于1的因子缩放时,触摸事件坐标会发生一定量的移动。我试图通过在CustomImageView类中的触摸位置绘制一个圆来模拟这个问题

                @Override
                    protected void onDraw(Canvas canvas) {
                        super.onDraw(canvas);
                        Paint paint = new Paint();
                        paint.setPathEffect(null);
                        paint.setStyle(Paint.Style.FILL_AND_STROKE);
                        paint.setColor(getResources().getColor(R.color.red));
                        if (mCirclePoint != null)
                            canvas.drawCircle(touchEvent.x, touchEvent.y, 6, paint);
                       }
该圆在画布的初始状态(mScalFactor=1)下绘制良好,但当按大于1的因子缩放时,其位置会从实际接触点移动。我已尝试使用矩阵解决此问题,如下所示

            public float[] getAbsolutePosition(float touchX, float touchY) {
                    float[] scaledPoints = new float[2];
                    scaledPoints[0] = touchX ;
                    scaledPoints[1] = touchY;
                    Matrix matrix = new Matrix();
                    matrix.reset();
                    foat scaleFactor = mCustomFrameLayout.getScaleFactor();
                    float centerX = mCustomFrameLayout.getCenterScaleX();
                    float centerY = mCustomFrameLayout.getCenterScaleY();
                    matrix.setScale(scaleFactor, scaleFactor, centerX, centerY);
                    matrix.mapPoints(scaledPoints);
                   return scaledPoints;
              }
但最终也会遇到同样的问题


你能帮我解决这个问题吗?我在谷歌上搜索了很多,但没有完美的解决方案。*

你不应该使用画布来缩放,最好使用矩阵

要从图像(非屏幕)获取触摸坐标,请执行以下操作:

要获取屏幕坐标,请使用:

e.getX();
e.getY();

在onDraw中使用canvas.getMatrix,该矩阵及其逆矩阵可用于映射点显示您是否获得触摸坐标?@Salauyou:Through onTouchEvent()…event.getX()或getRawX()?@Salauyou:event.getX()。。。
void coorImagen(MotionEvent e){
    float []m = new float[9];
    matrix.getValues(m);
    float transX = m[Matrix.MTRANS_X] * -1;
    float transY = m[Matrix.MTRANS_Y] * -1;
    float scaleX = m[Matrix.MSCALE_X];
    float scaleY = m[Matrix.MSCALE_Y];
    lastTouchX = (int) ((e.getX() + transX) / scaleX);
    lastTouchY = (int) ((e.getY() + transY) / scaleY);
    lastTouchX = Math.abs(lastTouchX);
    lastTouchY = Math.abs(lastTouchY);

}
e.getX();
e.getY();