Code example for AnimationSet

Methods: addAnimation, setAnimationListener, setDuration, setFillAfter

0
	} 
	 
	long rotationRate = 1000+(long)(Math.random()*1202);
	 
	public void animatePWIcon() { 
		AnimationSet anims = new AnimationSet(false);
		long duration = (long)(1502*Math.random()+700);
		float rotTimes = (float)duration/(float)rotationRate;
		float newRotation = rotation+
				((float)rotateDirection*(360f*rotTimes));
		float nextRotation = newRotation-(((int)(newRotation/360))*360);
		if (Math.abs(rotTimes-(int)rotTimes)<.25 && rotTimes>.5) {
			newRotation = newRotation-nextRotation;
			nextRotation=0;
		} 
		RotateAnimation rotate = new RotateAnimation(rotation, newRotation, 
				RotateAnimation.RELATIVE_TO_SELF, rotateX, RotateAnimation.RELATIVE_TO_SELF, rotateY);
		if (nextRotation==0) {
			if (Math.random()>.5)
				rotateX=.7f+(float)(Math.random()*.5);
			else 
				rotateX=.3f-(float)(Math.random()*.5);
			if (Math.random()>.5)
				rotateY=.7f+(float)(Math.random()*.5);
			else 
				rotateY=.3f-(float)(Math.random()*.5);
		}  
		if (stoppedRotation) {
			rotate.setInterpolator(AnimationUtils.loadInterpolator(getActivity(), android.R.anim.accelerate_interpolator));
			stoppedRotation = false;
		} else if (Math.random()>.82) {
			rotateDirection *= -1;
			rotate.setInterpolator(AnimationUtils.loadInterpolator(getActivity(), android.R.anim.decelerate_interpolator));
			stoppedRotation=true;
		} else 
			rotate.setInterpolator(AnimationUtils.loadInterpolator(getActivity(), android.R.anim.linear_interpolator));
		anims.addAnimation(rotate);
		rotation = nextRotation;
		 
		float newX=getRandomNewPosition();
		float newY=getRandomNewPosition();
		anims.addAnimation(new TranslateAnimation(
				TranslateAnimation.RELATIVE_TO_PARENT, x, TranslateAnimation.RELATIVE_TO_PARENT, newX,
				TranslateAnimation.RELATIVE_TO_PARENT, y, TranslateAnimation.RELATIVE_TO_PARENT, newY));
		x = newX;
		y = newY;
		anims.setFillAfter(true);
		anims.setDuration(duration);
		anims.setAnimationListener(new AnimationListener() {
			@Override 
			public void onAnimationEnd(Animation arg0) {
				PinkwertherSubstantialFragment.this.animatePWIcon();
			} 
			@Override