ScaleAnimationアニメーションの使用方法

ScaleAnimationはAndroidにおける、Viewの大きさを変化させてアニメーション効果を実現するクラスです。使い方は次の通りです。

  1. ScaleAnimationオブジェクトの作成:ScaleAnimationオブジェクトを作成するには、ScaleAnimationのコンストラクタを用いて、スケールの開始サイズと終了サイズを指定します。
ScaleAnimation scaleAnimation = new ScaleAnimation(fromX, toX, fromY, toY);

其中、fromXとfromYは拡大アニメーションの開始サイズ、toXとtoYは拡大アニメーションの終了サイズである。それらの値は絶対値でも、ビューの幅と高さに対する比でもかまわない。

  1. アニメーションの属性の設定:ScaleAnimation メソッドにより、アニメーションの継続時間、繰り返し回数、インターポレーターなどのアニメーションの属性を設定できます。
scaleAnimation.setDuration(duration);
scaleAnimation.setRepeatCount(repeatCount);
scaleAnimation.setInterpolator(interpolator);

そのうち、durationはアニメーションの継続時間でミリ秒単位です。repeatCountはアニメーションの繰り返しの数で、Animation.INFINITEに設定すると無限に繰り返されます。interpolatorはアニメーションのインターポレータで、アニメーションの変化の速度を制御します。

  1. ViewのstartAnimationメソッドを使用してアニメーション効果を適用する
view.startAnimation(scaleAnimation);

なお、viewはアニメーション効果をかけるViewオブジェクト。

完全なサンプルコードを以下に示します。

ScaleAnimation scaleAnimation = new ScaleAnimation(fromX, toX, fromY, toY);
scaleAnimation.setDuration(duration);
scaleAnimation.setRepeatCount(repeatCount);
scaleAnimation.setInterpolator(interpolator);
view.startAnimation(scaleAnimation);

以上の手順により、ScaleAnimation の使用が可能になります。実際の使用では、必要に応じて拡大縮小の開始および終了サイズと、アニメーションの属性を調整して、必要なアニメーション効果を得ることができます。

bannerAds