Scaling image of ImageView while maintaining center point in same place

那年仲夏 提交于 2019-11-29 23:33:01

问题


I have set a prescaled Bitmap as ImageView's source. Then I've read Matrix of an ImageView and shift Bitmap of an ImageView via matrix.postTranslate(shiftX, shiftY).

Now I want to zoom in / out and image while maintaining center of ImageView at the same point of Bitmap that was before scale.

If I try to zoom in an image with matrix.postScale(zoom, zoom), point that I want to maintain (blue dot) shifts to other place (purple dot).

I have tried several different ways to shift Bitmap back, but I cant get it to work correctly. I know initial Bitmap size, ImageView size, distances marked by doted line. Tried to calculate needed shift and use matrix.postTranslate(-zoomshiftX, -zoomshiftY) afterwards, but it doesn't shift correctly.

Even found out, that underlying Bitmap's pixel count doesnt change after matrix.postScale() function and tried matrix.postTranslate(-zoomshiftX/zoom, -zoomshiftY/zoom) - but still no luck.

How do I achieve such zoom?


回答1:


Take a look at my question here regarding creating a zoomable ViewGroup. I've described code snippets from my end solution, and some of it might be helpful.

Extending RelativeLayout, and overriding dispatchDraw() to create a zoomable ViewGroup




回答2:


Maybe this can be helpful:

If you got two fingers on screen, you can get the event of the two fingers and get the mid point:

PointF mid;
MotionEvent event;
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
mid.set(x / 2, y / 2);

Then you can set the scalation having the mid point in the center of the screen:

matrix.postScale(scale, scale, mid.x, mid.y);


来源:https://stackoverflow.com/questions/6624103/scaling-image-of-imageview-while-maintaining-center-point-in-same-place

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