Java I';我很难理解什么是PhotoView';s getDisplayRect()实际返回(构建android照片裁剪工具)
我正试图使用该库为照片构建裁剪工具,但我无法理解Java I';我很难理解什么是PhotoView';s getDisplayRect()实际返回(构建android照片裁剪工具),java,android,math,Java,Android,Math,我正试图使用该库为照片构建裁剪工具,但我无法理解getDisplayRect()返回的值。我将照片设置在图像视图上,如下所示: photo.setImageDrawable(new BitmapDrawable(getResources(), image)); 其中,image是位图对象。然后设置一些缩放值: float minScale = ((float)image.getWidth() > (float)image.getHeight()) ? (float)image.g
getDisplayRect()
返回的值。我将照片设置在图像视图上,如下所示:
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
对象
用户完成后,我使用以下命令确定位图在图像视图中可见的部分:
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);
但我得到了意想不到的结果。也许这里有人能提供一些见解?以下是答案:
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;
}
rectfrect=attacher.getDisplayRect();
float viewScale=attacher.getScale();
float imageRatio=(float)size.width()/(float)size.height();
float viewRatio=(float)photoView.getWidth()/(float)photoView.getHeight();
浮动比例=0;
如果(imageRatio>viewRatio){
//比例基于图像宽度
scale=1/((float)size.width()/(float)photoView.getWidth()/viewScale);
}否则{
//比例基于图像高度,或1
scale=1/((float)size.height()/(float)photoView.getHeight()/viewScale);
}
//转换为位图比例
rect.left=-rect.left/比例;
rect.top=-rect.top/比例;
rect.right=rect.left+((float)photoView.getWidth()/scale);
rect.bottom=rect.top+((float)photoView.getHeight()/scale);
如果(rect.top)什么是size.width()和size.height()?@user3809445我猜size变量指的是PhotoView中显示的原始图像文件的位图。