之前用 AnimatedImage 时一直对 AnimatedSprite 很奇怪,想试一下怎么用,一下子没试出来,放下了,后来一直没时间。 OK ,今天想起来,又搞了一下。
AnimatedSprite 元素用来播放精灵动画。
一些常见的属性解释:
方法:
还有一些属性,看帮助吧。
AnimatedSprite 元素使用的图片格式有特别的要求。下面是我们示例用的图片:
图片需要平铺所有的帧(和GIF不同),其实这里的帧就是图片里的一个区域。平铺的顺序是从左到右、从上到下。你指定了 frameX 、 frameY 、 frameWidth 、 frameHeight 、 frameCount ,AnimatedSprite 就会分析你提供的图片,生成相关帧的信息。
创建了一个 Qt Quick Application ,播放 numbers.png ,效果看起来就像电影前面的倒计时数字那样。
效果如图:
main.qml 代码都在这里:
import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Controls 1.2
Window {
visible: true;
width: 360;
height: 320;
color: "black";
AnimatedSprite {
id: animated;
width: 64;
height: 64;
anchors.centerIn: parent;
source: "qrc:/numbers.png";
frameWidth: 64;
frameHeight: 64;
frameDuration: 200;
frameCount: 10;
frameX: 0;
frameY: 0;
onCurrentFrameChanged: {
info.text = "%1/%2".arg(animated.currentFrame).arg(animated.frameCount);
}
}
Row{
spacing: 4;
anchors.horizontalCenter: parent.horizontalCenter;
anchors.bottom: parent.bottom;
anchors.bottomMargin: 4;
Text {
id: info;
width: 60;
height: 24;
color: "red";
verticalAlignment: Text.AlignVCenter;
horizontalAlignment: Text.AlignRight;
}
Button {
width: 60;
height: 24;
text: (animated.paused == true) ? "Play" : "Pause";
onClicked: (animated.paused == true) ? animated.resume() : animated.pause();
}
Button {
width: 70;
height: 24;
text: "Advance";
onClicked: animated.advance();
}
Button {
width: 70;
height: 24;
text: "Restart";
onClicked: animated.restart();
}
Button {
width: 60;
height: 24;
text: "Quit";
onClicked: Qt.quit();
}
}
}回顾一下我的Qt Quick系列文章:
原文地址:http://blog.csdn.net/foruok/article/details/42233365