/*Google AdSense自動広告*/

2014年6月23日月曜日

鈴を振るアニメーション(RotateAnimation)を自然に動かす

Tokiu AlarmsではアラームのON/OFFをトグルボタンで実装していますが、そのON時に鈴を振るアニメーション効果を加えています。
RotateAnimationでマイナスからプラスの角度に回転させれば良いのですが、アニメーションが終わった後に一瞬で元の画像に戻るため不自然さが出てしまいます。
そこで、アニメーションの終了時に次のアニメーションに移行し、そこでゆっくりと元の画像に戻すようにしました。setAnimationListenerでonAnimationEndに次のアニメーションを入れてやるだけです。またその中に別のアニメーションを入れれば、次々と違ったアニメーションを作動されることができます(汚いコードになりそうですが・・・)。
原理的にはアニメーションスタート時も、一瞬で角度が変わるため不自然に見える筈ですが、大して気にならないのはタップした指で隠れるからでしょう。



alarmButton.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View arg0) {
    if (alarmButton.isChecked()) {
     StartRingAnimation();
    }
   }
public void StartRingAnimation() {
  int randDuration = (int)(Math.random() * 200) - 100;
  
  RotateAnimation rotateAnimation = new RotateAnimation(-10, 10, 1, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF);
  rotateAnimation.setDuration(300 + randDuration);
  rotateAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
  rotateAnimation.setRepeatCount(5);
  rotateAnimation.setRepeatMode(Animation.REVERSE);
  rotateAnimation.setFillAfter(true);
  rotateAnimation.setAnimationListener(new AnimationListener() {
   @Override
   public void onAnimationEnd(Animation animation) {
    RotateAnimation returnAnimation = new RotateAnimation(-10, 0, 1, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF);
    returnAnimation.setDuration(1000);
    returnAnimation.setInterpolator(new DecelerateInterpolator());
    alarmButton.clearAnimation();
    alarmButton.startAnimation(returnAnimation);
   }

   @Override
   public void onAnimationRepeat(Animation animation) {}
   @Override
   public void onAnimationStart(Animation animation) {}
   
  });
  alarmButton.startAnimation(rotateAnimation);
 }
 
public void StopRingAnimation() {
  alarmButton.clearAnimation();
 }

0 件のコメント:

コメントを投稿