[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4vKlbCug-1646114478688)(//upload-images.jianshu.io/upload_images/15266155-8e5454f434b43798?imageMogr2/auto-orient/strip%7CimageView2/2/w/360/format/webp)]
1.将extended_image的mode参数设置为ExtendedImageMode.Gesture
2.设置GestureConfig
ExtendedImage.network(
imageTestUrl,
fit: BoxFit.contain,
//enableLoadState: false,
mode: ExtendedImageMode.Gesture,
gestureConfig: GestureConfig(
minScale: 0.9,
animationMinScale: 0.7,
maxScale: 3.0,
animationMaxScale: 3.5,
speed: 1.0,
inertialSpeed: 100.0,
initialScale: 1.0,
inPageView: false),
)
GestureConfig 参数说明
参数 | 描述 | 默认值 |
---|---|---|
minScale | 缩放最小值 | 0.8 |
animationMinScale | 缩放动画最小值,当缩放结束时回到minScale值 | minScale * 0.8 |
maxScale | 缩放最小值 | 5.0 |
animationMaxScale | 缩放动画最大值,当缩放结束时回到maxScale值 | maxScale * 1.2 |
speed | 缩放拖拽速度,与用户操作成正比 | 1.0 |
inertialSpeed | 拖拽惯性速度,与惯性速度成正比 | 100 |
cacheGesture | 是否缓存手势状态,可用于Pageview中保留状态,使用clearGestureDetailsCache方法清除 | false |
inPageView | 是否使用ExtendedImageGesturePageView展示图片 | false |
这一个功能比较简单,参考了官方的gestures demo,将缩放的Scale和Offset转换了为了图片最后显示的区域,具体代码在最后绘制图片的时候,将gestureDetails转换为对应的图片显示区域。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Hl39qvyu-1646114478689)(//upload-images.jianshu.io/upload_images/15266155-56cf480a703e1fd1?imageMogr2/auto-orient/strip%7CimageView2/2/w/329/format/webp)]
bool gestureClip = false;
if (gestureDetails != null) {
destinationRect =
gestureDetails.calculateFinalDestinationRect(rect, destinationRect);
///outside and need clip
gestureClip = outRect(rect, destinationRect);
if (gestureClip) {
canvas.save();
canvas.clipRect(rect);
}
}
rect 是整个图片在屏幕上的区域,destinationRect图片显示区域(会根据BoxFit的不同而所不同),通过gestureDetails的calculateFinalDestinationRect方式,计算出最终显示区域。
1.根据缩放点相对图片的位置对缩放点作为中心点进行缩放
2.如果Scale小于等于1.0的时候,按照图片的中心点进行缩放的,而当大于1.0并且图片已经铺满区域的时候按照1来执行
3.当图片是那种长宽相差很大的时候,进行缩放的时候,将首先沿着比较长的那边进行中心点缩放,直到图片铺满区域之后,按照1来执行
4.当进行缩放操作的时候,不进行移动操作
1,2,3对应代码
Offset _getCenter(Rect destinationRect) {
if (!userOffset && _center != null) {
return _center;
}
if (totalScale > 1.0) {
if (_computeHorizontalBoundary && _computeVerticalBoundary) {
return destinationRect.center * totalScale + offset;
} else if (_computeHorizontalBoundary) {
//only scale Horizontal
return Offset(destinationRect.center.dx * totalScale,
destinationRect.center.dy) +
Offset(offset.dx, 0.0);
} else if (_computeVerticalBoundary) {
//only scale Vertical
return Offset(destinationRect.center.dx,
destinationRect.center.dy * totalScale) +
Offset(0.0, offset.dy);
} else {
return destinationRect.center;
}
} else {
return destinationRect.center;
}
}
4对应代码,当details.scale==1.0,说明是一个移动操作,否则为了一个缩放操作
void _handleScaleUpdate(ScaleUpdateDetails details) {
…
var offset =
((details.scale == 1.0 ? details.focalPoint : _startingOffset) -
_normalizedOffset * scale);
…
}
获取到了图片的中心点之后,我们再根据Scale等到图片的整个区域
Rect _getDestinationRect(Rect destinationRect, Offset center) {
final double width = destinationRect.width * totalScale;
final double height = destinationRect.height * totalScale;
return Rect.fromLTWH(
center.dx - width / 2.0, center.dy - height / 2.0, width, height);
}
1.计算是否需要计算限制边界
2.如果需要将区域限制在边界内部
if (_computeHorizontalBoundary) {
//move right
if (result.left >= layoutRect.left) {
result = Rect.fromLTWH(0.0, result.top, result.width, result.height);
_boundary.left = true;
}
///move left
if (result.right <= layoutRect.right) {
result = Rect.fromLTWH(layoutRect.right - result.width, result.top,
result.width, result.height);
_boundary.right = true;
}
}
if (_computeVerticalBoundary) {
//move down
if (result.bottom <= layoutRect.bottom) {
result = Rect.fromLTWH(result.left, layoutRect.bottom - result.height,
result.width, result.height);
_boundary.bottom = true;
}
//move up
if (result.top >= layoutRect.top) {
result = Rect.fromLTWH(
result.left, layoutRect.top, result.width, result.height);
_boundary.top = true;
}
}
_computeHorizontalBoundary =
result.left <= layoutRect.left && result.right >= layoutRect.right;
_computeVerticalBoundary =
result.top <= layoutRect.top && result.bottom >= layoutRect.bottom;
void _handleScaleEnd(ScaleEndDetails details) {
//animate back to maxScale if gesture exceeded the maxScale specified
if (_gestureDetails.totalScale > _gestureConfig.maxScale) {
final double velocity =
(_gestureDetails.totalScale - _gestureConfig.maxScale) /
_gestureConfig.maxScale;
_gestureAnimation.animationScale(
_gestureDetails.totalScale, _gestureConfig.maxScale, velocity);
return;
}
//animate back to minScale if gesture fell smaller than the minScale specified
if (_gestureDetails.totalScale < _gestureConfig.minScale) {
final double velocity =
(_gestureConfig.minScale - _gestureDetails.totalScale) /
_gestureConfig.minScale;
_gestureAnimation.animationScale(
_gestureDetails.totalScale, _gestureConfig.minScale, velocity);
return;
}
if (_gestureDetails.gestureState == GestureState.pan) {
// get magnitude from gesture velocity
final double magnitude = details.velocity.pixelsPerSecond.distance;
// do a significant magnitude
if (magnitude >= minMagnitude) {
final Offset direction = details.velocity.pixelsPerSecond /
magnitude *
_gestureConfig.inertialSpeed;
_gestureAnimation.animationOffset(
_gestureDetails.offset, _gestureDetails.offset + direction);
}
}
}
唯一注意的是Scale的回弹动画将以最后的缩放中心点为中心进行缩放,这样缩放动画才看起来舒服一些
//true: user zoom/pan
//false: animation
final bool userOffset;
Offset _getCenter(Rect destinationRect) {
if (!userOffset && _center != null) {
return _center;
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KfbeUtJl-1646114478690)(//upload-images.jianshu.io/upload_images/15266155-4dcccaff2e1d4669?imageMogr2/auto-orient/strip%7CimageView2/2/w/292/format/webp)]
1.使用
an
//false: animation
final bool userOffset;
Offset _getCenter(Rect destinationRect) {
if (!userOffset && _center != null) {
return _center;
}
[外链图片转存中…(img-KfbeUtJl-1646114478690)]
1.使用