QML:如何在图像源之间产生良好的过渡效果(一个淡入另一个)?

时间:2012-04-19 08:36:58

标签: qt qml

我想知道如何在QML中的图像源之间进行平滑过渡,我试试

import QtQuick 1.1
Image {
   id: rect
   source:  "quit.png"
   smooth: true
   MouseArea {
       id: mouseArea
       anchors.fill: parent
       anchors.margins: -10
       hoverEnabled: true         //this line will enable mouseArea.containsMouse
       onClicked: Qt.quit()
   }

    states: State {
        name: "mouse-over"; when: mouseArea.containsMouse
        PropertyChanges { target: rect; scale: 0.8; source :"quit2.png" }
    }

    transitions: Transition {
        NumberAnimation { properties: "scale, source"; easing.type: Easing.InOutQuad; duration: 1000  }
    }
}

但是它不像源代码那样在源上作为最终状态改变而工作。所以我想知道如何让一个图像源淡入到其他和后面?

2 个答案:

答案 0 :(得分:9)

您希望第一张图片淡入另一张图片吗?如果将两个Image个对象放在一起,然后为opacity属性设置动画,该怎么办?

编辑:这对我有用(我使用的是QtQuick 1.0,因为我的Qt Creator安装有点过时了):

import QtQuick 1.0
Rectangle {
Image {
   id: rect
   source:  "quit.png"
   smooth: true
   opacity: 1
   MouseArea {
       id: mouseArea
       anchors.fill: parent
       anchors.margins: -10
       hoverEnabled: true         //this line will enable mouseArea.containsMouse
       onClicked: Qt.quit()
   }


    states: State {
        name: "mouse-over"; when: mouseArea.containsMouse
        PropertyChanges { target: rect; scale: 0.8; opacity: 0}
        PropertyChanges { target: rect2; scale: 0.8; opacity: 1}
    }

    transitions: Transition {
        NumberAnimation { properties: "scale, opacity"; easing.type: Easing.InOutQuad; duration: 1000  }
    }
}

Image {
   id: rect2
   source:  "quit2.png"
   smooth: true
   opacity: 0
   anchors.fill: rect

  }
}

对于评论中的问题:您可以通过anchors.fill: rect

复制锚点,将图像准确地放在另一个上面

答案 1 :(得分:0)

这里也是图像之间的简单滚动过渡:

import QtQuick 2.6
import QtQuick.Controls 1.4
import QtQuick.Window 2.2

Window {
  visible: true
  width: 640
  height: 480
  title: qsTr("Hello World")



  Rectangle {
    id: imageRect

    anchors.centerIn: parent
    width: 240
    height: 320
    clip: true

    property int currentIndex: 0
    property var imageSources: [ "imageLeft.jpg", "imageCenter.jpg" ]

    Repeater {
      model: imageRect.imageSources

      Image {
        id: image

        width: parent.width
        height: parent.height

        x: index * parent.width - imageRect.currentIndex * parent.width
        fillMode: Image.PreserveAspectFit
        source: imageRect.imageSources[index]
        Behavior on x { SpringAnimation { spring: 2; damping: 0.2 } }
      }
    }
  }

  Button {
    id: leftButton
    anchors.bottom: parent.bottom
    text: "left"
    onClicked: if(imageRect.currentIndex > 0) imageRect.currentIndex--
  }

  Button {
    id: rightButton
    anchors.bottom: parent.bottom
    anchors.left: leftButton.right
    text: "right"
    onClicked: if(imageRect.currentIndex < imageRect.imageSources.length - 1) imageRect.currentIndex++
  }

  Button {
    id: addButton
    anchors.bottom: parent.bottom
    anchors.left: rightButton.right
    text: "+"
    onClicked: imageRect.imageSources = [ "imageLeft.jpg", "imageCenter.jpg" , "imageRight.jpg" ]
  }
}