2012-06-18 15 views
2

我正在尋找一個正確的方式來實現「ScaleAnimation」。我的目的是一個動畫QImage的:縮放在QGraphicsItemAnimation

timeline = new QTimeLine(time); 
    timeline->setFrameRange(0, 100); 
    animation = new QGraphicsItemAnimation; 

    QRectF rect = item->boundingRect(); 
    int h = rect.bottom() - rect.top(); 
    int w = rect.right() - rect.left(); 


    animation->setItem(item); 
    animation->setTimeLine(timeline); 


    for (int i = 0; i < 100; i++) { 
      int x = w + (int)((float)w*(float)(i/100.0)); 
      qreal xx = (qreal)(x)/(qreal)w; 
      int y = (h) + (int)((float)h*(float)(i/100.0)); 
      qreal yy = (qreal)(y)/(qreal)h;   
      animation->setScaleAt(i/100, xx, yy); 
    } 

似乎工作,但動畫的起源似乎是(0, 0)。有什麼辦法可以在(w/2, h/2)中應用動畫嗎?有更好更有效率(或正確)的方式來重寫動畫嗎?我在Qt世界退出了新手。

感謝您的耐心等待。

回答

0

如果您使用的是QGraphicsPixmapItem,只需將其偏移量設置爲中點,然後將其移動相同的量即可抵消設置偏移量的效果。

const QSizeF size = item->boundingRect().size()*0.5; 
item->setOffset(size.width(), size.height()); 
item->moveBy(size.width(), size.height()); 
+0

這是不正確的。您正在移動項目,而不是應用動畫的點。 – Blackbelt

+0

你試過了嗎?動畫圍繞位置(0,0)縮放 - 只要您的0,0碰巧位於中心位置,您應該可以。試試看看。 –

+0

是的,我嘗試過。我的中心位於w/2,h/2 – Blackbelt