在Android中的imageview内获取触摸屏位置

时间:2014-11-19 23:49:47

标签: android matrix imageview ontouch

我正在研究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)相对于屏幕坐标;好的:我想将这些坐标对应于图像。

此图片也可以解释我想要的内容:

enter image description here

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

0 个答案:

没有答案