简体   繁体   English

Android自定义imageview具有类似webview的缩放

[英]Android custom imageview with webview-like scaling

i think this question has been asked quite often, but I couldn`t find an appropriate solution for my implementation. 我认为这个问题经常被问到,但是我找不到适合我的实现的解决方案。 I built an custom imageview with an onScaleListener and an onGestureListener that scales and pans the containing image. 我用一个onScaleListener和一个onGestureListener构建了一个自定义的imageview,它缩放和平移包含的图像。 The scaling is done with a matrix scaling. 缩放是通过矩阵缩放完成的。 The function looks like that: 该函数如下所示:

    @Override
    public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
            scaleFactor *= scaleGestureDetector.getScaleFactor();
            scaleFactor = Math.max(initScale, Math.min(scaleFactor, initScale + 3.0f));
            matrix = getImageMatrix();
            matrix.setScale(scaleFactor, scaleFactor);
            matrix.getValues(values);
            matrix.postTranslate(-values[Matrix.MTRANS_X] + Math.max(0, centerX - centerImageX),
                    -values[Matrix.MTRANS_Y] + Math.max(0, centerY - centerImageY));
            setImageMatrix(matrix);
}

postTranslate() is needed to center the image if needed. 如果需要,需要postTranslate()来使图像居中。 To finish this I need to scroll to (scrollTo(x,y)) the position where the focus of the scaling gesture stays in the same position on the screen. 为此,我需要滚动到(scrollTo(x,y))缩放手势的焦点保持在屏幕上相同位置的位置。 At the end it should look like scaling in a webview. 最后,它看起来像在Web视图中缩放。 Can anybody help me with this? 有人可以帮我吗?
When I use: 当我使用时:
float scrollPosX = ((scrollwidth) * ((getScrollX() + touchX) / imagewidth)); float scrollPosX =((滚动宽度)*((getScrollX()+ touchX)/ imagewidth));
float scrollPosY = ((scrollheight) * ((getScrollY() + touchY) / imageheight)); 浮动scrollPosY =((scrollheight)*((getScrollY()+ touchY)/ imageheight));
it will work for the first scaling, but when scaling in a scaled image it will scroll to the relative position. 它适用于第一次缩放,但是在缩放的图像中缩放时,它将滚动到相对位置。 I think it is all related to the fact that I only get the touch position with getScrollX() and getScrollY() [it`s difficult to explain] 我认为这与以下事实有关:我只能通过getScrollX()getScrollY()获得触摸位置[这很难解释]

Now I found a solution that works for me. 现在,我找到了适合我的解决方案。 In my case I have to use the scaling step instead of the scaling factor. 就我而言,我必须使用缩放步骤而不是缩放因子。

                scrollTo((int) (getScrollX() - x + (scaleGestureDetector.getScaleFactor() * x) ),
                         (int) (getScrollY() - y + (scaleGestureDetector.getScaleFactor() * y) ));

with x and y as the touch position on the scaled image. 使用x和y作为缩放图像上的触摸位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM