6

我正在尝试使用PhotoView库为照片构建裁剪工具,但我无法理解getDisplayRect(). 我将照片设置为ImageView这样:

photo.setImageDrawable(new BitmapDrawable(getResources(), image));

image位图对象在哪里。然后我设置了一些缩放值:

float minScale = ((float)image.getWidth() > (float)image.getHeight())
    ? (float)image.getWidth() / (float)image.getHeight()
    : (float)image.getHeight() / (float)image.getWidth();
attacher.setMaxScale(minScale * 5f);
attacher.setMidScale(minScale * 2.5f);
attacher.setMinScale(minScale);
attacher.setScale(minScale, (float)image.getWidth() / 2f,(float)image.getHeight() / 2f, false);
attacher.update();

attacher对象在哪里PhotoViewAttacher

当用户完成后,我使用以下内容来确定位图中可见的部分ImageView

RectF rect      = attacher.getDisplayRect();
float scale             = attacher.getScale();
PhotoData ret   = new PhotoData(data);
ret.x       = (int)(Math.abs(rect.left) / scale);
ret.y       = (int)(Math.abs(rect.top) / scale);
ret.width   = (int)(rect.width() / scale);
ret.height  = (int)(rect.height() / scale);

我得到了意想不到的结果。也许这里有人可以提供一些见解?

4

1 回答 1

6

这是答案:

            RectF rect          = attacher.getDisplayRect();
            float viewScale     = attacher.getScale();

            float imageRatio = (float)size.width() / (float)size.height();
            float viewRatio = (float)photoView.getWidth() / (float)photoView.getHeight();

            float scale = 0;
            if (imageRatio > viewRatio) {
                // scale is based on image width
                scale = 1 / ((float)size.width() / (float)photoView.getWidth() / viewScale);

            } else {
                // scale is based on image height, or 1
                scale = 1 / ((float)size.height() / (float)photoView.getHeight() / viewScale);
            }

            // translate to bitmap scale
            rect.left       = -rect.left / scale;
            rect.top        = -rect.top / scale;
            rect.right      = rect.left + ((float)photoView.getWidth() / scale);
            rect.bottom     = rect.top + ((float)photoView.getHeight() / scale);

            if (rect.top<0) {
                rect.bottom -= Math.abs(rect.top);
                rect.top = 0;
            }
            if (rect.left<0) {
                rect.right -= Math.abs(rect.left);
                rect.left = 0;
            }
于 2013-09-14T20:52:17.620 回答