How to magnify/zoom part of image

后端 未结 1 1379
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 07:36

I\'m making an app where user will be able to click on part of the image and get a magnified version in the corner of WebView. I managed to make a Paint

1条回答
  •  爱一瞬间的悲伤
    2021-02-04 07:58

    Seems that the issue is with how you are using the matrix.

    Now you are using the original image (1) as a shader which is then being post scaled up around a pivot point (2), which is like doing a zoom around a point (3) - but not centering the point (4) ! (For example, open google maps and zoom in on the map with your mouse - the point is zoomed around the pivot but the pivot is not centered)

    What will be an easier way to achieve what you want is by using the Rect to Rect method. I.E. you want to take a small area from the original image (5) and draw it to a larger area (6) .

    And here is a code sample:

    RectF src = new RectF(zoomPos.x-50, zoomPos.y-50, zoomPos.x+50, zoomPos.y+50);
    RectF dst = new RectF(0, 0, 200, 200);
    matrix.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER);
    

    Some more points:

    • Try not to create new object in the onTouch - it is being called many times and it is not good on performance . Instead, create once and reuse.
    • getAction() will have issues when there are more than one finger on screen since it is the action ID and the pointer ID. Instead use getActionMasked() and getActionIndex().
    • Do not use hardcoded values (50/100 in the sample code) - these are pixels and are not aware of screen density. Use scaled size like dp.

    0 讨论(0)
提交回复
热议问题