set Animation to ImageView - Android User Interface

Android examples for User Interface:ImageView

Description

set Animation to ImageView

Demo Code


//package com.java2s;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

public class Main {
  public static int repeatCount;

  public static void setAnimation(final ImageView img_heart) {

    final ScaleAnimation animation1 = new ScaleAnimation(1.0f, 1.4f, 1.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f);
    animation1.setDuration(1000);//from   w  w w  .  java2 s  . c  o  m

    final ScaleAnimation animation2 = new ScaleAnimation(1.4f, 1.0f, 1.4f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f);
    animation2.setDuration(1000);

    animation1.setAnimationListener(new AnimationListener() {
      @Override
      public void onAnimationStart(Animation animation) {
      }
      @Override
      public void onAnimationRepeat(Animation animation) {
      }
      @Override
      public void onAnimationEnd(Animation animation) {
        img_heart.setAnimation(animation2);
        animation2.startNow();
      }
    });

    animation2.setAnimationListener(new AnimationListener() {
      @Override
      public void onAnimationStart(Animation animation) {
      }
      @Override
      public void onAnimationRepeat(Animation animation) {
      }
      @Override
      public void onAnimationEnd(Animation animation) {
        repeatCount++;
        if (repeatCount <= 2) {
          img_heart.setAnimation(animation1);
          animation1.start();
        }
      }
    });
    img_heart.setAnimation(animation1);
    animation1.start();
  }
}

Related Tutorials