Get Canvas coordinates after scaling up/down or dragging in android

[亡魂溺海] 提交于 2019-11-27 11:36:20
Awais Tariq

Done it finally by myself.

Draw everything by applying this formula to (px,py) coordinates:

float px = ev.getX() / mScaleFactor + rect.left;
float py = ev.getY() / mScaleFactor + rect.top;
rect = canvas.getClipBounds();
//Get them in on Draw function and apply above formula before drawing
Awais Tariq
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    clipBounds_canvas = canvas.getClipBounds();
    /////Do whatever you want to do..!!!            
};

@Override
public boolean onTouchEvent(MotionEvent ev) {
    int x = ev.getX() / zoomFactor + clipBounds_canvas.left;
    int y = ev.getY() / zoomFactor + clipBounds_canvas.top;
    //Use the above two values insted of ev.getX() and ev.getY();
}

Hope this will help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!