[Qt] アイテムのスケールをアニメーションで変化させるには?QGraphicsItemAnimation::setScaleAt()徹底解説


QGraphicsItemAnimation::setScaleAt() 関数は、QGraphicsItem アニメーションにおいて、特定のステップにおけるアイテムのスケールを設定するために使用されます。この関数は、アイテムの x 軸方向と y 軸方向のスケールを個別に設定できます。

構文

void QGraphicsItemAnimation::setScaleAt(qreal step, qreal sx, qreal sy);

パラメータ

  • sy: アイテムの y 軸方向のスケール。1.0 は元のスケール、それ以外はその倍率を表します。
  • sx: アイテムの x 軸方向のスケール。1.0 は元のスケール、それ以外はその倍率を表します。
  • step: アニメーションのステップ値。0.0 は開始、1.0 は終了を表します。
// アイテムを 2 倍に拡大するアニメーションを作成します。

QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
animation->setItem(item); // アニメーション対象のアイテムを設定

// アニメーションのステップ 0.5 でアイテムを 2 倍に拡大
animation->setScaleAt(0.5, 2.0, 2.0);

// アニメーションを実行
animation->start();
  • アイテムのスケールを設定するには、QTransform オブジェクトを使用することもできます。ただし、QGraphicsItemAnimation を使用すると、より柔軟でアニメーションしやすいコードになります。
  • setScaleAt() 関数は、アニメーションの タイムライン に基づいてスケールが変化します。デフォルトのタイムラインは線形ですが、カスタムのタイムラインを設定することもできます。
  • 上記以外にも、QGraphicsItemAnimation クラスには様々な機能があります。詳細については、Qt ドキュメントを参照してください。


この例では、アイテムを 5 倍に縮小してから 0 倍に拡大するアニメーションを作成します。

#include <QGraphicsItemAnimation>
#include <QGraphicsItem>

int main() {
  // アイテムを作成
  QGraphicsItem *item = new QGraphicsItem;

  // アニメーションを作成
  QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
  animation->setItem(item);

  // アニメーションのステップ 0.25 でアイテムを 0.5 倍に縮小
  animation->setScaleAt(0.25, 0.5, 0.5);

  // アニメーションのステップ 0.75 でアイテムを 2.0 倍に拡大
  animation->setScaleAt(0.75, 2.0, 2.0);

  // アニメーションを実行
  animation->start();

  return 0;
}

例2: アイテムを回転しながら拡大・縮小するアニメーション

この例では、アイテムを回転しながら 5 倍に縮小してから 0 倍に拡大するアニメーションを作成します。

#include <QGraphicsItemAnimation>
#include <QGraphicsItem>

int main() {
  // アイテムを作成
  QGraphicsItem *item = new QGraphicsItem;

  // アニメーションを作成
  QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
  animation->setItem(item);

  // アニメーションのステップ 0.25 でアイテムを 0.5 倍に縮小し、45 度回転
  animation->setScaleAt(0.25, 0.5, 0.5);
  animation->setRotationAt(0.25, 45);

  // アニメーションのステップ 0.75 でアイテムを 2.0 倍に拡大し、90 度回転
  animation->setScaleAt(0.75, 2.0, 2.0);
  animation->setRotationAt(0.75, 90);

  // アニメーションを実行
  animation->start();

  return 0;
}

例3: アイテムを波打たせるアニメーション

この例では、アイテムを x 軸方向と y 軸方向に波打たせるアニメーションを作成します。

#include <QGraphicsItemAnimation>
#include <QGraphicsItem>
#include <QTimeLine>

int main() {
  // アイテムを作成
  QGraphicsItem *item = new QGraphicsItem;

  // アニメーションを作成
  QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
  animation->setItem(item);

  // タイムラインを作成
  QTimeLine *timeline = new QTimeLine(1000); // アニメーション時間 (ミリ秒)

  // x 軸方向のスケールを波打たせる
  for (int i = 0; i <= 100; ++i) {
    qreal scaleX = 1.0 + 0.2 * sin(2 * M_PI * i / 100);
    animation->setScaleAt(i / 100.0, scaleX, 1.0);
  }

  // y 軸方向のスケールを波打たせる
  for (int i = 0; i <= 100; ++i) {
    qreal scaleY = 1.0 + 0.2 * sin(2 * M_PI * i / 100);
    animation->setScaleAt(i / 100.0, 1.0, scaleY);
  }

  // タイムラインとアニメーションを関連付ける
  timeline->start();
  animation->start(timeline);

  return 0;
}
  • アニメーションの効果をより複雑にするために、様々な QGraphicsItemAnimation 関数と組み合わせることができます。
  • 上記の例はあくまで基本的な例です。ご自身のニーズに合わせてカスタマイズすることができます。


QGraphicsItemAnimation::setScaleAt() 関数は、QGraphicsItem アニメーションにおいて、特定のステップにおけるアイテムのスケールを設定するために使用されます。しかし、状況によっては、他の方法でアイテムのスケールを設定することがより適切な場合があります。

代替方法

  • QPropertyAnimation クラスを使用する:**

    QPropertyAnimation クラスを使用して、アイテムの scale プロパティをアニメーション化することができます。

    // アイテムを 0.5 倍から 2.0 倍に拡大するアニメーション
    
    QGraphicsItem *item = new QGraphicsItem;
    
    QPropertyAnimation *animation = new QPropertyAnimation(item, "scale");
    animation->setDuration(1000); // アニメーション時間 (ミリ秒)
    animation->setStartValue(QPointF(0.5, 0.5));
    animation->setEndValue(QPointF(2.0, 2.0));
    animation->start();
    
  • QTransform オブジェクトを使用する:**

    QGraphicsItem クラスは、setTransform() メソッドを使用して QTransform オブジェクトを設定することができます。QTransform オブジェクトには、アイテムのスケール、回転、移動などの様々な変換を適用することができます。

    // アイテムを 2 倍に拡大する
    
    QGraphicsItem *item = new QGraphicsItem;
    
    QTransform transform;
    transform.scale(2.0, 2.0);
    item->setTransform(transform);
    
  • カスタムアニメーションを実装する

    • 最も柔軟性があり、あらゆる種類のアニメーションを作成できる
    • 複雑で、より多くのコードを書く必要がある
  • QPropertyAnimation クラスを使用する:**

    • アニメーション化が簡単
    • Qt アニメーションフレームワークの機能を利用できる
    • QGraphicsItem クラスの scale プロパティしかアニメーション化できない
  • QTransform オブジェクトを使用する:**

    利点:

    • シンプルで使いやすい
    • 柔軟性があり、様々な変換を適用できる

    欠点:

    • アニメーション化するには、追加のコードが必要になる