Как установить максимальный / минимальный зум мерцания - PullRequest
0 голосов
/ 06 сентября 2018

Я пытаюсь сделать масштабирование Item (Image или любой другой компонент) и сделать его для планшета. В настоящее время мой код выглядит так:

Rectangle {
    id: root
    anchors.fill: parent
    color: mainWindow.toGradient
    Flickable {
        id: flick
        anchors.fill: parent
        contentWidth: width
        contentHeight: height
        boundsBehavior: Flickable.StopAtBounds

        PinchArea {
            id: pArea
            width: Math.max(flick.contentWidth, flick.width)
            height: Math.max(flick.contentHeight, flick.height)
            property real initialWidth
            property real initialHeight
            onPinchStarted: {
                initialWidth = flick.contentWidth
                initialHeight = flick.contentHeight
            }
            onPinchUpdated: {
                flick.contentX += pinch.previousCenter.x - pinch.center.x
                flick.contentY += pinch.previousCenter.y - pinch.center.y

                flick.resizeContent(initialWidth * pinch.scale, initialHeight * pinch.scale, pinch.center)
            }

            onPinchFinished: {
                flick.returnToBounds()
            }

            Rectangle {
                width: flick.contentWidth
                height: flick.contentHeight
                color: "black"
                Image {
                    anchors.fill: parent
                    source: "qrc:/image.jpg"
                }

                MouseArea {
                    anchors.fill: parent
                    onDoubleClicked: {
                        flick.contentWidth = root.width
                        flick.contentHeight = root.height
                    }
                }
            }
        }
    }
}

Работает хорошо, но когда все закончится, я все равно могу уменьшить масштаб (сделать его меньше, родитель).

Как мне остановить масштабирование (и, если возможно, увеличение) в некоторой точке / масштабе?

Спасибо за вашу помощь!

1 Ответ

0 голосов
/ 11 сентября 2018

В конце концов, я с энтузиазмом удалил Flickable и вместо этого масштабировал Item.

Rectangle {
    id: root
    anchors.fill: parent
    color: "black"

    Item {
        id: photoFrame
        width: root.width
        height: root.height
        Image { 
            id: image
            anchors.fill: parent
            source: "image.jpg"
        }

        PinchArea {
            anchors.fill: parent
            pinch.target: photoFrame
            pinch.minimumRotation: 0 //Rotation (i guess we don't need to rotate it)
            pinch.maximumRotation: 0
            pinch.minimumScale: 1 //Minimum zoom for camera
            pinch.maximumScale: 4 //Maximum zoom for camera

            onPinchUpdated: { //Dont zoom behind border
                if(photoFrame.x < dragArea.drag.minimumX)
                    photoFrame.x = dragArea.drag.minimumX
                else if(photoFrame.x > dragArea.drag.maximumX)
                    photoFrame.x = dragArea.drag.maximumX

                if(photoFrame.y < dragArea.drag.minimumY)
                    photoFrame.y = dragArea.drag.minimumY
                else if(photoFrame.y > dragArea.drag.maximumY)
                    photoFrame.y = dragArea.drag.maximumY
            }

            MouseArea {
                id: dragArea
                hoverEnabled: true
                anchors.fill: parent
                drag.target: photoFrame
                scrollGestureEnabled: false  // 2-finger-flick gesture should pass through to the Flickable
                drag.minimumX: (root.width - (photoFrame.width * photoFrame.scale))/2
                drag.maximumX: -(root.width - (photoFrame.width * photoFrame.scale))/2
                drag.minimumY: (root.height - (photoFrame.height * photoFrame.scale))/2
                drag.maximumY: -(root.height - (photoFrame.height * photoFrame.scale))/2

                onDoubleClicked: { //Reset size and location of camera
                    photoFrame.x = 0
                    photoFrame.y = 0
                    photoFrame.scale = 1
                }
                onWheel: {
                    var scaleBefore = photoFrame.scale
                    photoFrame.scale += photoFrame.scale * wheel.angleDelta.y / 120 / 10
                    if(photoFrame.scale < 1)
                        photoFrame.scale = 1
                    else if(photoFrame.scale > 4)
                        photoFrame.scale = 4

                    if(photoFrame.x < drag.minimumX)//Dont zoom behind border of image
                        photoFrame.x = drag.minimumX
                    else if(photoFrame.x > drag.maximumX)
                        photoFrame.x = drag.maximumX

                    if(photoFrame.y < drag.minimumY)
                        photoFrame.y = drag.minimumY
                    else if(photoFrame.y > drag.maximumY)
                        photoFrame.y = drag.maximumY
                }
            }
        }
    }
}
...