Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QML : Fade in / Fade out animation for Image Element

Tags:

animation

qml

Is it possible to have a fade in/out animation when the source of an Image element is being changed ? Do I need two Image elements ? Changing the opacity of one of the from 0 to 1 and another from 1 to 0 ?

like image 756
s4eed Avatar asked Sep 15 '12 12:09

s4eed


2 Answers

To do this without much hassle. Run animations in this way:

Image {
    id: toBeCreated
    NumberAnimation on opacity {
        id: createAnimation
        from: 0
        to: 1
        duration: 2000
    }
    Component.onCompleted: createAnimation.start()
}

Image {
    id: toBeDeleted
    NumberAnimation on opacity {
        id: destroyAnimation // start this animation from the function where you want to create new Images
        to: 0
        duration: 2000
        onRunningChanged: {
             if (!running) {
                 console.log("Destroying...")
                 toBeDeleted.destroy();
             }
        }
    }    
}
like image 91
RajaRaviVarma Avatar answered Oct 18 '22 03:10

RajaRaviVarma


I Know its a little late but felt like sharing

Inspired by RajaRaviVarma ans i tried something like

A Qml for FadeInOut ImageView

import QtQuick 2.0

    Item {

        property string imageSource : ""
         property string imageSourceTemp : ""

        property real parentWidth: 0
        property real parentHeight: 0

        onImageSourceChanged:
        {

            destroyAnimation.start()
            createAnimation.start()


        }
        Image {
            id: toBeDeleted
            source: imageSourceTemp
            width: parentWidth
            height: parentHeight

            NumberAnimation on opacity {
                id: destroyAnimation
                to: 0.5
                duration: 400
                onRunningChanged: {
                     if (!running) {

                     }
                }
            }
        }

        Image {
            id: toBeCreated
            source: imageSource
            width: parentWidth
            height: parentHeight

            NumberAnimation on opacity {
                id: createAnimation
                from: 0
                to: 1
                duration: 800

                onRunningChanged: {
                     if (!running) {
                        imageSourceTemp = imageSource
                     }
                }
            }


        }

    }

And to use it Like

 FadeinFadeOutImage {
        id: song_image
        imageSource: songImage
        parentWidth: width
        parentHeight: height
        width: 406*scaleFactor
        height: 406*scaleFactor   
    }
like image 43
George Thomas Avatar answered Oct 18 '22 03:10

George Thomas