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

前端 未结 2 1383
终归单人心
终归单人心 2020-12-02 13:40

I\'m developing an application in which I\'m pasting images, doing drawing and painting on canvas. This app can also Scale up/down the canvas or drag it to different locatio

相关标签:
2条回答
  • 2020-12-02 14:07
    @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.

    0 讨论(0)
  • 2020-12-02 14:16

    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
    
    0 讨论(0)
提交回复
热议问题