MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { // ----------------------------------------------------------------------- ui->setupUi(this); QGraphicsScene *scene = new QGraphicsScene(this); ui->graphicsView->setScene(scene); // ----------------------------------------------------------------------- // kolor pedzla QBrush brush = QBrush(Qt::red); brush.setStyle(Qt::DiagCrossPattern); // tworzymy obiekts QGraphicsRectItem *rect = new QGraphicsRectItem(10, 10, 90, 90); rect->setBrush(brush); // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- // definiujemy czas trwania animacji QTimeLine *timeLine = new QTimeLine(1000); timeLine->setFrameRange(0, 100); // definiujemy animacje QGraphicsItemAnimation *animation = new QGraphicsItemAnimation(); animation->setItem(rect); animation->setTimeLine(timeLine); // animacja int odcinek = 100; for (int i = 0; i < 100; ++i) animation->setPosAt(i / 100.0, QPointF(i, i)); // uruchamiamy scenę i animację scene->addItem(rect); timeLine->start(); }
EnemyShip::EnemyShip() { QGraphicsItem *spaceshipTail = new EnemyShipTail(this); QGraphicsItem *oneWing = new EnemyShipWing1(this); QGraphicsItem *secondWing = new EnemyShipWing2(this); QGraphicsItem *spaceshipBody = new EnemyShipBody(this); QGraphicsItem *spaceshipWindow = new EnemyShipWindow(this); spaceshipTail->setPos(602, 293); oneWing->setPos(565, 310); secondWing->setPos(623, 310); spaceshipBody->setPos(600, 300); spaceshipWindow->setPos(607, 332); timeline = new QTimeLine; QGraphicsItemAnimation *tailAnim = new QGraphicsItemAnimation; tailAnim->setItem(spaceshipTail); tailAnim->setTimeLine(timeline); tailAnim->setTranslationAt(0, 20, 0); tailAnim->setTranslationAt(1, -20, 0); QGraphicsItemAnimation *oneWingAnim = new QGraphicsItemAnimation; oneWingAnim->setItem(oneWing); oneWingAnim->setTimeLine(timeline); oneWingAnim->setTranslationAt(0, 20, 0); oneWingAnim->setTranslationAt(1, -20, 0); QGraphicsItemAnimation *secondWingAnim = new QGraphicsItemAnimation; secondWingAnim->setItem(secondWing); secondWingAnim->setTimeLine(timeline); secondWingAnim->setTranslationAt(0, 20, 0); secondWingAnim->setTranslationAt(1, -20, 0); QGraphicsItemAnimation *bodyAnim = new QGraphicsItemAnimation; bodyAnim->setItem(spaceshipBody); bodyAnim->setTimeLine(timeline); bodyAnim->setTranslationAt(0, 20, 0); bodyAnim->setTranslationAt(1, -20, 0); QGraphicsItemAnimation *windowAnim = new QGraphicsItemAnimation; windowAnim->setItem(spaceshipWindow); windowAnim->setTimeLine(timeline); windowAnim->setTranslationAt(0, 20, 0); windowAnim->setTranslationAt(1, -20, 0); timeline->setUpdateInterval(1000/25); timeline->setCurveShape(QTimeLine::SineCurve); timeline->setLoopCount(0); timeline->setDuration(2000); timeline->start(); }
int main(int argv, char *args[]) { QApplication app(argv, args); //! [0] QGraphicsItem *ball = new QGraphicsEllipseItem(0, 0, 20, 20); QTimeLine *timer = new QTimeLine(5000); timer->setFrameRange(0, 100); QGraphicsItemAnimation *animation = new QGraphicsItemAnimation; animation->setItem(ball); animation->setTimeLine(timer); for (int i = 0; i < 200; ++i) animation->setPosAt(i / 200.0, QPointF(i, i)); QGraphicsScene *scene = new QGraphicsScene(); scene->setSceneRect(0, 0, 250, 250); scene->addItem(ball); QGraphicsView *view = new QGraphicsView(scene); view->show(); timer->start(); //! [0] return app.exec(); }
QTimeLine* newAnim(Line* ball, int in, int to, QEasingCurve curve) { QTimeLine *NewTimer = new QTimeLine(speed); NewTimer->setEasingCurve(curve); QGraphicsItemAnimation* anim = new QGraphicsItemAnimation(); anim->setItem(ball); anim->setTimeLine(NewTimer); anim->setRotationAt(0, in); anim->setRotationAt(1, to); return NewTimer; }
void tst_QGraphicsItemAnimation::setTimeLine() { QGraphicsItemAnimation animation; QCOMPARE(animation.timeLine(), (QTimeLine *)0); QPointer<QTimeLine> line1 = new QTimeLine; animation.setTimeLine(line1); QCOMPARE(animation.timeLine(), (QTimeLine *)line1); animation.setTimeLine(line1); QVERIFY(line1); QCOMPARE(animation.timeLine(), (QTimeLine *)line1); animation.setTimeLine(0); QCOMPARE(animation.timeLine(), (QTimeLine *)0); QVERIFY(!line1); QTimeLine *line2 = new QTimeLine; animation.setTimeLine(line2); QCOMPARE(animation.timeLine(), (QTimeLine *)line2); delete line2; QCOMPARE(animation.timeLine(), (QTimeLine *)0); }
void MainWindow::slotAddAnimationItem() //在场景中加入一个动画星星 { StartItem *item = new StartItem; QGraphicsItemAnimation *anim = new QGraphicsItemAnimation; anim->setItem(item); QTimeLine *timeLine = new QTimeLine(4000); timeLine->setCurveShape(QTimeLine::SineCurve); timeLine->setLoopCount(0); anim->setTimeLine(timeLine); int y =(qrand()%400)-200; for(int i=0; i<400; i++) { anim->setPosAt(i/400.0,QPointF(i-200,y)); } timeLine->start(); scene->addItem(item); }
MyScene::MyScene(qreal x,qreal y,qreal width,qreal height,QObject *parent):QGraphicsScene(x,y,width,height,parent) { // 預計加一個圓形,然後,執徑逐漸變大 QGraphicsEllipseItem *ball = new QGraphicsEllipseItem(-15,-15,30,30); ball->setPen(QPen(QColor(125,125,125,125),3)); addItem(ball); ball->setPos(this->width()/2,this->height()/2); // setPos(x,y):設定此小座標的原點在此(x,y) timeLine = new QTimeLine; QGraphicsItemAnimation *headAnimation = new QGraphicsItemAnimation; headAnimation->setItem(ball); headAnimation->setTimeLine(timeLine); headAnimation->setScaleAt(1,10,10); timeLine->setUpdateInterval(1000 / 25); timeLine->setCurveShape(QTimeLine::SineCurve); timeLine->setLoopCount(0); timeLine->setDuration(2000); timeLine->start(); }
Robot::Robot() { QGraphicsItem *torsoItem = new RobotTorso(this); QGraphicsItem *headItem = new RobotHead(torsoItem); QGraphicsItem *upperLeftArmItem = new RobotLimb(torsoItem); QGraphicsItem *lowerLeftArmItem = new RobotLimb(upperLeftArmItem); QGraphicsItem *upperRightArmItem = new RobotLimb(torsoItem); QGraphicsItem *lowerRightArmItem = new RobotLimb(upperRightArmItem); QGraphicsItem *upperRightLegItem = new RobotLimb(torsoItem); QGraphicsItem *lowerRightLegItem = new RobotLimb(upperRightLegItem); QGraphicsItem *upperLeftLegItem = new RobotLimb(torsoItem); QGraphicsItem *lowerLeftLegItem = new RobotLimb(upperLeftLegItem); headItem->setPos(0, -18); upperLeftArmItem->setPos(-15, -10); lowerLeftArmItem->setPos(30, 0); upperRightArmItem->setPos(15, -10); lowerRightArmItem->setPos(30, 0); upperRightLegItem->setPos(10, 32); lowerRightLegItem->setPos(30, 0); upperLeftLegItem->setPos(-10, 32); lowerLeftLegItem->setPos(30, 0); timeLine = new QTimeLine; QGraphicsItemAnimation *headAnimation = new QGraphicsItemAnimation; headAnimation->setItem(headItem); headAnimation->setTimeLine(timeLine); headAnimation->setRotationAt(0, 20); headAnimation->setRotationAt(1, -20); headAnimation->setScaleAt(1, 1.1, 1.1); QGraphicsItemAnimation *upperLeftArmAnimation = new QGraphicsItemAnimation; upperLeftArmAnimation->setItem(upperLeftArmItem); upperLeftArmAnimation->setTimeLine(timeLine); upperLeftArmAnimation->setRotationAt(0, 190); upperLeftArmAnimation->setRotationAt(1, 180); QGraphicsItemAnimation *lowerLeftArmAnimation = new QGraphicsItemAnimation; lowerLeftArmAnimation->setItem(lowerLeftArmItem); lowerLeftArmAnimation->setTimeLine(timeLine); lowerLeftArmAnimation->setRotationAt(0, 50); lowerLeftArmAnimation->setRotationAt(1, 10); QGraphicsItemAnimation *upperRightArmAnimation = new QGraphicsItemAnimation; upperRightArmAnimation->setItem(upperRightArmItem); upperRightArmAnimation->setTimeLine(timeLine); upperRightArmAnimation->setRotationAt(0, 300); upperRightArmAnimation->setRotationAt(1, 310); QGraphicsItemAnimation *lowerRightArmAnimation = new QGraphicsItemAnimation; lowerRightArmAnimation->setItem(lowerRightArmItem); lowerRightArmAnimation->setTimeLine(timeLine); lowerRightArmAnimation->setRotationAt(0, 0); lowerRightArmAnimation->setRotationAt(1, -70); QGraphicsItemAnimation *upperLeftLegAnimation = new QGraphicsItemAnimation; upperLeftLegAnimation->setItem(upperLeftLegItem); upperLeftLegAnimation->setTimeLine(timeLine); upperLeftLegAnimation->setRotationAt(0, 150); upperLeftLegAnimation->setRotationAt(1, 80); QGraphicsItemAnimation *lowerLeftLegAnimation = new QGraphicsItemAnimation; lowerLeftLegAnimation->setItem(lowerLeftLegItem); lowerLeftLegAnimation->setTimeLine(timeLine); lowerLeftLegAnimation->setRotationAt(0, 70); lowerLeftLegAnimation->setRotationAt(1, 10); QGraphicsItemAnimation *upperRightLegAnimation = new QGraphicsItemAnimation; upperRightLegAnimation->setItem(upperRightLegItem); upperRightLegAnimation->setTimeLine(timeLine); upperRightLegAnimation->setRotationAt(0, 40); upperRightLegAnimation->setRotationAt(1, 120); QGraphicsItemAnimation *lowerRightLegAnimation = new QGraphicsItemAnimation; lowerRightLegAnimation->setItem(lowerRightLegItem); lowerRightLegAnimation->setTimeLine(timeLine); lowerRightLegAnimation->setRotationAt(0, 10); lowerRightLegAnimation->setRotationAt(1, 50); QGraphicsItemAnimation *torsoAnimation = new QGraphicsItemAnimation; torsoAnimation->setItem(torsoItem); torsoAnimation->setTimeLine(timeLine); torsoAnimation->setRotationAt(0, 5); torsoAnimation->setRotationAt(1, -20); timeLine->setUpdateInterval(1000 / 25); timeLine->setCurveShape(QTimeLine::SineCurve); timeLine->setLoopCount(0); timeLine->setDuration(2000); timeLine->start(); }