Code example for AnimationSet

Methods: getAnimations, setFillAfter, start

0
		}		 
	} 
 
	 
	private void animateChange(ArrayList<TilePosition> positions) {
		AnimationSet animationSet = new AnimationSet(true);
		DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator();
		 
		for (int i = 0; i < tiles.size(); i++) {
			AnimationSet scaleAndMove = new AnimationSet(true);
			scaleAndMove.setFillAfter(true);
 
			final SingleTileLayout currentTile = tiles.get(i);
			TilePosition currentPosition = currentTile.getPosition(); 
			final TilePosition targetPosition = positions.get(i);
			 
			if (currentPosition == null) {
				AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
				alphaAnimation.setDuration(animatedTransitionDuration);
				alphaAnimation.setStartOffset(0);
				currentTile.setAnimation(alphaAnimation);
				 
				scaleAndMove.addAnimation(alphaAnimation);
			} 
 
			currentTile.setPosition(targetPosition);
 
			if (!targetPosition.equals(currentPosition)) {
				float toXDelta = 0, toYDelta = 0;
				if (currentPosition != null) {
					// Calculate new position 
					toXDelta = targetPosition.getX() - currentPosition.getX();
					toYDelta = targetPosition.getY() - currentPosition.getY();
					 
					// Factor in the scaling animation 
					toXDelta = toXDelta / (targetPosition.getWidth() / currentPosition.getWidth());
					toYDelta = toYDelta / (targetPosition.getHeight() / currentPosition.getHeight());
				} 
				 
				// Move 
				TranslateAnimation moveAnimation = new TranslateAnimation(0, toXDelta, 0, toYDelta);
				moveAnimation.setDuration(animatedTransitionDuration);
				moveAnimation.setStartOffset(0);
				moveAnimation.setFillAfter(true);
				moveAnimation.setInterpolator(decelerateInterpolator);
				scaleAndMove.addAnimation(moveAnimation);
				 
				// Physically move the tile when the animation ends 
				scaleAndMove.setAnimationListener(new AnimationListener() {
					 
					@Override 
					public void onAnimationStart(Animation animation) {	}
					 
					@Override 
					public void onAnimationRepeat(Animation animation) { }
					 
					@Override 
					public void onAnimationEnd(Animation animation) {
						FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
								(int)targetPosition.getWidth(), 
								(int)targetPosition.getHeight(), 
								Gravity.TOP + Gravity.LEFT);
						lp.setMargins((int)targetPosition.getX(), 
								(int)targetPosition.getY(), 0, 0);
						 
						currentTile.setLayoutParams(lp);
						 
						// The following null animation just gets rid of screen flicker 
						animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f);
						animation.setDuration(1);
						currentTile.startAnimation(animation);
					} 
				}); 
				 
				// Scale 
				if (currentPosition != null) {
					ScaleAnimation scaleAnimation = 
						new ScaleAnimation(1, 
								targetPosition.getWidth() / currentPosition.getWidth(), 
								1,  
								targetPosition.getHeight() / currentPosition.getHeight(),
								Animation.ABSOLUTE, 0,
								Animation.ABSOLUTE, 0);
					 
					scaleAnimation.setDuration(animatedTransitionDuration);
					scaleAnimation.setStartOffset(0);
					scaleAnimation.setFillAfter(true);
					scaleAndMove.addAnimation(scaleAnimation);
				} 
				 
				// Set animation to the tile 
				currentTile.setAnimation(scaleAndMove);
			} 
			// Add to the total animation set 
			animationSet.addAnimation(scaleAndMove);
		} 
		 
		if (animationSet.getAnimations().size() > 0) {
			Log.d(TAG, "Starting animation");
			animationSet.setFillAfter(true);
			animationSet.start();
		} 
	} 
 
	 
}