1

我正在开发 Android 项目。
我正在使用自定义 View 类来显示图像并使用矩阵(移动、缩放、旋转)图像。
这是我正在使用的代码:

public class CustomView extends View implements OnTouchListener {

    private Bitmap bitmap;
    private float imgWidth;
    private float imgHeight;
    private float screenWidth;
    private float screenHeight;
    private Matrix matrix = new Matrix();
    private Vector2D position = new Vector2D();
    private float scale=1;
    private float angle = 0;
    private TouchManager touchManager = new TouchManager(2);
    private boolean isInitialized = false;
    // Debug helpers to draw lines between the two touch points
    Vector2D vca;
    Vector2D vcb;
    Vector2D vpa;
    Vector2D vpb;
    int PressCount=0;
    Paint paint;

    public CustomView(Context context, Bitmap bitmap) 
    {       
        super(context);
        this.bitmap = bitmap;
        this.imgWidth = bitmap.getWidth();
        this.imgHeight = bitmap.getHeight();
        setOnTouchListener(this);
    }

    private float GetTheta(){
        return (float) (angle*180/Math.PI);
    }


    @Override 
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        if (!isInitialized) 
        {
            screenWidth = getWidth();
            screenHeight = getHeight();
            position.set(screenWidth / 2, screenHeight / 2);
            scale=1;
            paint=new Paint();
            isInitialized = true;       
        }

        // Drawing the image
        matrix.reset();
        matrix.postTranslate(-imgWidth / 2.0f, -imgHeight / 2.0f);
        matrix.postRotate(GetTheta());
        matrix.postScale(scale, scale);
        matrix.postTranslate(position.getX(), position.getY());
        canvas.drawBitmap(bitmap, matrix, paint);

    }


    Vector2D DistancePoint;
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        vca = null;
        vcb = null;
        vpa = null;
        vpb = null;

        try {
            touchManager.update(event);
            PressCount=touchManager.getPressCount();
            if (PressCount == 1) 
            {
                vca = touchManager.getPoint(0);
                vpa = touchManager.getPreviousPoint(0);
                DistancePoint=touchManager.moveDelta(0);                
                position.add(DistancePoint);
            }
            else if (PressCount == 2)
            {
                    vca = touchManager.getPoint(0);
                    vpa = touchManager.getPreviousPoint(0);
                    vcb = touchManager.getPoint(1);
                    vpb = touchManager.getPreviousPoint(1);

                    Vector2D current = touchManager.getVector(0, 1);
                    Vector2D previous = touchManager.getPreviousVector(0, 1);
                    float currentDistance = current.getLength();
                    float previousDistance = previous.getLength();

                    if (previousDistance != 0) 
                    {
                        scale *= currentDistance / previousDistance;
                    }                           
                    angle -= Vector2D.getSignedAngleBetween(current, previous);
            }


            invalidate();
        }
        catch(Throwable t) {
            // So lazy...
        }
        return true;
    }

}

上面的代码运行完美!
我的问题是如何将屏幕触摸坐标转换为图像坐标?
换句话说:当触摸屏幕时,(event.getX, event.getY) 是相对于屏幕坐标的;好的:我想将这些坐标对应于相对于图像。

这张图片也可以解释我到底想要什么:

在此处输入图像描述

如有任何疑问,请评论我。
将不胜感激任何帮助。

4

0 回答 0