android - 根据矩阵将触摸值转换为点

标签 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);
}

最佳答案

我知道这是一篇旧帖子,但我遇到了同样的问题。

我有一张应用了矩阵变换的图像,首先调整它的大小,然后进行平移。

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

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

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

关于android - 根据矩阵将触摸值转换为点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9016230/

相关文章:

android - 预取图像是否仍应延迟加载到 ListView 中?

c++ - 一个管理多维数组的类!如何管理单元格中的不同数据类型?

r - R 中的 chol() 函数不断返回上三角(我需要下三角)

android - 捏缩放后获得正确的坐标(画线)Android Canvas

android - 如何让 root 为 Android 中的系统应用程序工作?

android - 带燃气表的 Tesseract OCR

java - Google Speech-to-Text API - Android 应用程序崩溃

r - 通过在 R 中将对角线元素添加为 1,从 n-1*n 矩阵创建 n*n 矩阵

android - 位图在Android中是如何存储在内存中的?

android - 如何在 Google map 上放置图钉以及如何在 Google map 上的对话框中获取当前地址?