缩放ImageView的图像,同时保持中心点在同一位置[英] Scaling image of ImageView while maintaining center point in same place

本文是小编为大家收集整理的关于缩放ImageView的图像,同时保持中心点在同一位置的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我已将预定位图设置为imageView的源.然后我读取了通过matrix.postTranslate(shiftX, shiftY)读取ImageView的矩阵和图像视图的移位位图.

现在我想放大/缩小和图像,同时在比例之前的位图的相同点保持ImageView的中心.

如果我尝试用matrix.postScale(zoom, zoom)缩放图像,指向我想要维护的点(蓝点)转移到其他地方(紫点).

我已经尝试了几种不同的方式来移回位图,但我不能让它正常工作.我知道初始位图大小,imageView大小,标记为Doted Line的距离.试图计算所需的换档并使用matrix.postTranslate(-zoomshiftX, -zoomshiftY)之后,但它不会正确换档.

甚至发现,底层位图的像素计数在matrix.postscale()函数和尝试矩阵之后没有更改.posttranslate(-zoomshiftx/zoom,-zoomshifty/zoom) - 但仍然没有运气.

如何实现这种缩放?

scalling问题

推荐答案

在此处查看我的问题关于创建缩放视图组.我已经描述了来自我的最终解决方案的代码片段,其中一些可能会有所帮助.

扩展relativelayout,并推翻dismatchdraw()以创建缩放viewgroup

其他推荐答案

可能会有所帮助:

如果在屏幕上有两个手指,则可以获得两个手指的活动并获得中点:

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);

然后,您可以在屏幕中心中设置具有中点的缩放:

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

本文地址:https://www.itbaoku.cn/post/102404.html

问题描述

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?

scalling issue

推荐答案

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

其他推荐答案

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);