将触摸值转换为基于矩阵的点

Ram*_*nki 2 android matrix android-canvas

我正在按矩阵翻译和缩放图像,现在我有矩阵值.所以从矩阵值我想将我的触摸坐标转换为它通过矩阵得到的位置.那我该怎么办呢?请尽快帮助我.

private void drawPoint(float x, float y, float pressure, float width) {

 // left is tx of matrix 
     // top is ty of matrix

    float curX = (x - left) / (scale * scale);
    float curY = (y - top) / (scale * scale);

         canvas.drawPoint((curX - left), (curY - top) , mPaint);
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ele 6

我知道这是一个老帖子,但我遇到了同样的问题.

我有一个图像应用矩阵变换,首先我调整它,然后我做翻译.

    image = new Matrix();
    image.setScale(zoom, zoom);

    Paint drawPaint = new Paint();
    drawPaint.setAntiAlias(true);
    drawPaint.setFilterBitmap(true);

    float centerScaledWidth = image_center.x * zoom / 2;
    float centerScaledHeigth = image_center.y * zoom / 2;

    image.postTranslate(screen_center.x -  centerScaledWidth, 
            screen_center.y - centerScaledHeigth);

    canvas.drawBitmap(bmp, image, drawPaint);
Run Code Online (Sandbox Code Playgroud)

为了得到图像上的点,我得到了矩阵图像的痕迹,这是方法:

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

            final int action = ev.getAction();

            switch (action & MotionEvent.ACTION_MASK) {

                    case MotionEvent.ACTION_DOWN: {

                        final float x = ev.getX();
                        final float y = ev.getY();

                        float[] pts = {x, y};

                        Matrix m = new Matrix();

                        // This is the magic, I set the new matrix m 
                        // as the inverse of 
                        // the matrix applied to the image
                        image.invert(m);

                        // transform the points using the inverse matrix
                        m.mapPoints(pts);

                        // do what you have to do
                        .......

                        Log.i("transformed", pts[0] +" "+pts[1]);

                        break;
                    }

            }

    return super.onTouchEvent(ev);
}
Run Code Online (Sandbox Code Playgroud)