這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)使用Flutter怎么實現(xiàn)一個可以縮放拖拽的圖片,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)公司一直秉承“誠信做人,踏實做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務(wù)為基礎(chǔ),以質(zhì)量求生存,以技術(shù)求發(fā)展,成交一個客戶多一個朋友!專注中小微企業(yè)官網(wǎng)定制,成都網(wǎng)站設(shè)計、成都網(wǎng)站制作,塑造企業(yè)網(wǎng)絡(luò)形象打造互聯(lián)網(wǎng)企業(yè)效應(yīng)。
主要功能:
縮放拖拽
在PageView里面縮放拖拽
用法
1.將extended_image的mode參數(shù)設(shè)置為ExtendedImageMode.Gesture
2.設(shè)置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 參數(shù)說明
| 參數(shù) | 描述 | 默認值 |
|---|---|---|
| minScale | 縮放最小值 | 0.8 |
| animationMinScale | 縮放動畫最小值,當縮放結(jié)束時回到minScale值 | minScale * 0.8 |
| maxScale | 縮放最小值 | 5.0 |
| animationMaxScale | 縮放動畫最大值,當縮放結(jié)束時回到maxScale值 | maxScale * 1.2 |
| speed | 縮放拖拽速度,與用戶操作成正比 | 1.0 |
| inertialSpeed | 拖拽慣性速度,與慣性速度成正比 | 100 |
| cacheGesture | 是否緩存手勢狀態(tài),可用于Pageview中保留狀態(tài),使用clearGestureDetailsCache方法清除 | false |
| inPageView | 是否使用ExtendedImageGesturePageView展示圖片 | false |
實現(xiàn)過程
這一個功能比較簡單,參考了官方的gestures demo,將縮放的Scale和Offset轉(zhuǎn)換了為了圖片最后顯示的區(qū)域,具體代碼在最后繪制圖片的時候,將gestureDetails轉(zhuǎn)換為對應(yīng)的圖片顯示區(qū)域。
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 是整個圖片在屏幕上的區(qū)域,destinationRect圖片顯示區(qū)域(會根據(jù)BoxFit的不同而所不同),通過gestureDetails的calculateFinalDestinationRect方式,計算出最終顯示區(qū)域。
讓縮放的過程看起來流暢
1.根據(jù)縮放點相對圖片的位置對縮放點作為中心點進行縮放
2.如果Scale小于等于1.0的時候,按照圖片的中心點進行縮放的,而當大于1.0并且圖片已經(jīng)鋪滿區(qū)域的時候按照1來執(zhí)行
3.當圖片是那種長寬相差很大的時候,進行縮放的時候,將首先沿著比較長的那邊進行中心點縮放,直到圖片鋪滿區(qū)域之后,按照1來執(zhí)行
4.當進行縮放操作的時候,不進行移動操作
1,2,3對應(yīng)代碼
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對應(yīng)代碼,當details.scale==1.0,說明是一個移動操作,否則為了一個縮放操作
void _handleScaleUpdate(ScaleUpdateDetails details) {
...
var offset =
((details.scale == 1.0 ? details.focalPoint : _startingOffset) -
_normalizedOffset * scale);
...
}獲取到了圖片的中心點之后,我們再根據(jù)Scale等到圖片的整個區(qū)域
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.如果需要將區(qū)域限制在邊界內(nèi)部
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;
}在PageView里面縮放拖拽

用法
1.使用
ExtendedImageGesturePageView展示圖片
2.設(shè)置GestureConfig的inPageView 為Ture
GestureConfig 參數(shù)說明
| 參數(shù) | 描述 | 默認值 |
|---|---|---|
| inPageView | 是否使用ExtendedImageGesturePageView展示圖片 | false |
實現(xiàn)過程
手勢沖突
這個場景需要關(guān)注的是手勢的沖突問題,PageView里面是有水平或者垂直的手勢的,會跟onScaleStart/onScaleUpdate/onScaleEnd有沖突。
最開始想的是手勢應(yīng)該有冒泡,是不是可以我監(jiān)聽到了之后,不像上冒泡,這樣可以阻止PageView里面的滑動行為,最后結(jié)論是沒有方法能阻止冒泡。
關(guān)于手勢,大家可以看看拉面小姐姐關(guān)于手勢的文章,神奇的競技場概念。。
既然不能阻止手勢冒泡,那么我就直接不讓你能滾動了,然后全部的手勢都交給我,我來處理。
首先我看了下PageView關(guān)于滾動的源碼,直接指向最終ScrollableState里面的代碼,在setCanDrag方法里面根據(jù)是否可以Drag,準備了水平/垂直的手勢。
把ScrollableState里面關(guān)于水平垂直滾動處理的代碼拿出來,我創(chuàng)建了一個屬于extended_image專門的extended_image_gesture_page_view,屬性跟PageView一樣只是沒法設(shè)置physics,
因為強制設(shè)置為了NeverScrollableScrollPhysics
Widget result = PageView.custom( scrollDirection: widget.scrollDirection, reverse: widget.reverse, controller: widget.controller, childrenDelegate: widget.childrenDelegate, pageSnapping: widget.pageSnapping, physics: widget.physics, onPageChanged: widget.onPageChanged, key: widget.key, ); result = RawGestureDetector( gestures: _gestureRecognizers, behavior: HitTestBehavior.opaque, child: result, );
然后我們通過RawGestureDetector來注冊_gestureRecognizers(水平/垂直的手勢)。
關(guān)于_gestureRecognizers,我之前一直好奇PageView里面有個手hold的操作是怎么做到了,直到看到源碼才知道這么個東西,源碼真是個好東西。
void _handleDragDown(DragDownDetails details) {
//print(details);
_gestureAnimation.stop();
assert(_drag == null);
assert(_hold == null);
_hold = position.hold(_disposeHold);
}到達邊界滾動上下一個圖片
有了之前縮放拖拽的基礎(chǔ),這部分就比較簡單了。如果到達邊界就是用默認代碼去操作PageView,否則就控制Image進行拖拽操作
void _handleDragUpdate(DragUpdateDetails details) {
// _drag might be null if the drag activity ended and called _disposeDrag.
assert(_hold == null || _drag == null);
var delta = details.delta;
if (extendedImageGestureState != null) {
var gestureDetails = extendedImageGestureState.gestureDetails;
if (gestureDetails != null) {
if (gestureDetails.movePage(delta)) {
_drag?.update(details);
} else {
extendedImageGestureState.gestureDetails = GestureDetails(
offset: gestureDetails.offset +
delta * extendedImageGestureState.imageGestureConfig.speed,
totalScale: gestureDetails.totalScale,
gestureDetails: gestureDetails);
}
} else {
_drag?.update(details);
}
} else {
_drag?.update(details);
}
}拖拽慣性效果
在DragEnd的時候,我們需要注意下處理下慣性。
當圖片是放大狀態(tài)而且水平或者垂直能夠滑動的時候,我們需要_drag停止下來,以防止直接滑動到上一個或者下一個圖片
DragEndDetails(primaryVelocity: 0.0),并且根據(jù)慣性讓圖片在范圍內(nèi)繼續(xù)慣性滑動。
void _handleDragEnd(DragEndDetails details) {
// _drag might be null if the drag activity ended and called _disposeDrag.
assert(_hold == null || _drag == null);
var temp = details;
if (extendedImageGestureState != null) {
var gestureDetails = extendedImageGestureState.gestureDetails;
if (gestureDetails != null && gestureDetails.computeHorizontalBoundary ||
gestureDetails.computeVerticalBoundary) {
//stop
temp = DragEndDetails(primaryVelocity: 0.0);
// get magnitude from gesture velocity
final double magnitude = details.velocity.pixelsPerSecond.distance;
// do a significant magnitude
if (magnitude >= minMagnitude) {
Offset direction = details.velocity.pixelsPerSecond /
magnitude *
(extendedImageGestureState.imageGestureConfig.inertialSpeed);
if (widget.scrollDirection == Axis.horizontal) {
direction = Offset(direction.dx, 0.0);
} else {
direction = Offset(0.0, direction.dy);
}
_gestureAnimation.animationOffset(
gestureDetails.offset, gestureDetails.offset + direction);
}
}
}
_drag?.end(temp);
assert(_drag == null);
}上述就是小編為大家分享的使用Flutter怎么實現(xiàn)一個可以縮放拖拽的圖片了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享文章:使用Flutter怎么實現(xiàn)一個可以縮放拖拽的圖片
當前URL:http://www.aaarwkj.com/article14/peggge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、面包屑導(dǎo)航、服務(wù)器托管、企業(yè)網(wǎng)站制作、虛擬主機、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)