start Animation for ImageView - Android User Interface

Android examples for User Interface:ImageView

Description

start Animation for ImageView

Demo Code



//package com.java2s;

import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class Main {
  public static void startAnimation(final ImageView imageView, int[] startPos, int[] endPos) {
    int x = startPos[0];
    int y = startPos[1];
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
    lp.leftMargin = x;// w w w.j  a v a2 s  .  c om
    lp.topMargin = y;
    imageView.setLayoutParams(lp);

    int endX = endPos[0] - startPos[0];
    int endY = endPos[1] - startPos[1];

    TranslateAnimation translateAnimationX = new TranslateAnimation(0, endX, 0, 0);
    translateAnimationX.setInterpolator(new LinearInterpolator());
    translateAnimationX.setRepeatCount(0);
    translateAnimationX.setFillAfter(true);

    TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, endY);
    translateAnimationY.setInterpolator(new AccelerateInterpolator());
    translateAnimationY.setRepeatCount(0);
    translateAnimationX.setFillAfter(true);

    AnimationSet set = new AnimationSet(false);
    set.setFillAfter(false);
    set.addAnimation(translateAnimationY);
    set.addAnimation(translateAnimationX);
    set.setDuration(800);

    imageView.setAnimation(set);

    set.setAnimationListener(new Animation.AnimationListener() {
      @Override
      public void onAnimationStart(Animation animation) {
        imageView.setVisibility(View.VISIBLE);
      }

      @Override
      public void onAnimationEnd(Animation animation) {
        imageView.setVisibility(View.GONE);
      }

      @Override
      public void onAnimationRepeat(Animation animation) {

      }
    });
  }
}

Related Tutorials