图像缩放的正确变换
我正在为 android 编写一个应用程序(尽管我认为这是一个通用问题),我需要显示一个可以滚动和缩放的大图像(在 ImageView 中)。我已经成功地通过捕获触摸事件和执行矩阵转换来实现滚动工作,现在我正在研究缩放。
如果我简单地对图像应用缩放变换,它会在原点(屏幕的左上角)处放大。我想放大屏幕中央。 根据我的阅读,这意味着我需要进行转换以使原点成为屏幕的中心。我认为所需要的类似于以下内容 - 为简单起见,假设屏幕中心是 (5, 5)...
- 翻译 (-5, -5)
-按缩放系数缩放
-按 (+5, +5)*缩放系数翻译 不幸的是
,这似乎不起作用——变焦似乎可以去任何地方,除了中心……有人可以帮我吗?
编辑:这是现在有效的代码
Matrix zoommatrix = new Matrix();
float[] centerpoint = {targetimageview.getWidth()/2.0f, targetimageview.getHeight()/2.0f};
zoommatrix.postScale(zoomfactor, zoomfactor, centerpoint[0], centerpoint[1]);
zoommatrix.preConcat(targetimageview.getImageMatrix());
targetimageview.setImageMatrix(zoommatrix);
targetimageview.invalidate();
I'm writing an app for android (although I think this is a generic question) and I need to display a large image (in an ImageView) that can be scrolled and zoomed. I've managed to get scrolling to work by capturing touch events and performing matrix translations, and i'm now working on zooming.
If I simply apply a scale transformation to the image, it zooms in at the origin, which is the top left-hand corner of the screen. I would like to zoom in at the center of the screen.
From what i've read, this means I need a transformation to make the origin the center of the screen. I think what is required is something like the following- assume the center of the screen is (5, 5) for simplicity...
-Translate by (-5, -5)
-Scale by the zoom factor
-Translate by (+5, +5)*zoomfactor
Unfortunatly, this doesnt seem to work- the zoom seems to go anywhere BUT the center...can someone help me out here?
EDIT: This is the code that now works
Matrix zoommatrix = new Matrix();
float[] centerpoint = {targetimageview.getWidth()/2.0f, targetimageview.getHeight()/2.0f};
zoommatrix.postScale(zoomfactor, zoomfactor, centerpoint[0], centerpoint[1]);
zoommatrix.preConcat(targetimageview.getImageMatrix());
targetimageview.setImageMatrix(zoommatrix);
targetimageview.invalidate();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查Android源代码的Camera应用程序中的ImageViewTouchBase;它的“zoomTo”方法是这样做的:
那个中心方法可能是你真正关心的:
Check ImageViewTouchBase in the Android source code's Camera app; its "zoomTo" method does this:
That center method is probably the bit you'll really care about: