[Java for Android App Development] Saurabh Shah:
Adding Aminations To Give Your Application A Cool Strike :good:

Here I have mentioned some Cool Animations Effects.

[Note: Assuming the package name to be package.name.here and created a folder named anim in /res/.

Blink Effect

[QUOTE]
[B]Java Code[/B] [BlinkActivity.java]

package package.name.here;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

public class BlinkActivity extends Activity implements AnimationListener {

TextView txtMessage;
Button btnStart;

// Animation
Animation animBlink;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blink);

txtMessage = (TextView) findViewById(R.id.txtMessage);
btnStart = (Button) findViewById(R.id.btnStart);

// load the animation
animBlink = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.blink);

// set animation listener
animBlink.setAnimationListener(this);

// button click event
btnStart.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
txtMessage.setVisibility(View.VISIBLE);

// start the animation
txtMessage.startAnimation(animBlink);
}
});

}

@Override
public void onAnimationEnd(Animation animation) {
// Take any action after completing the animation

// check for blink animation
if (animation == animBlink) {
}

}

@Override
public void onAnimationRepeat(Animation animation) {

}

@Override
public void onAnimationStart(Animation animation) {

}

}

[B]XML Code[/B] [activity_blink.xml in [COLOR="green"]res/layout/[/COLOR] folder]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView android:id="@+id/txtMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is fadein animation"
android:layout_centerInParent="true"
android:textSize="25dp"
android:padding="20dp"
android:background="#000000"
android:visibility="gone"/>

<Button android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_marginTop="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"/>

</RelativeLayout>

[B]XML Code[/B] [Blink.xml in [COLOR="green"]res/anim/[/COLOR] folder]

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="600"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>

[/QUOTE]

FadeOut Effect

[QUOTE]
[B]Java Code[/B] [FadeOutActivity.java]

package info.androidhive.androidanimations;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class FadeOutActivity extends Activity implements AnimationListener {

TextView txtMessage;
Button btnStart;

// Animation
Animation animFadeOut;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fadeout);

txtMessage = (TextView) findViewById(R.id.txtMessage);
btnStart = (Button) findViewById(R.id.btnStart);

// load the animation
animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_out);

// set animation listener
animFadeOut.setAnimationListener(this);

// button click event
btnStart.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// start the animation
txtMessage.startAnimation(animFadeOut);
}
});

}

@Override
public void onAnimationEnd(Animation animation) {
// Take any action after completing the animation

// check for fade out animation
if (animation == animFadeOut) {
Toast.makeText(getApplicationContext(), "Animation Stopped",
Toast.LENGTH_SHORT).show();
}

}

@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}

}
[B]XML Code[/B] [activity_fadeout.xml in [COLOR="green"]res/layout/[/COLOR] folder]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView android:id="@+id/txtMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is fadeout animation"
android:layout_centerInParent="true"
android:textSize="25dp"/>

<Button android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Animation"
android:layout_marginTop="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"/>

</RelativeLayout>

[B]XML Code[/B] [fade_out.xml in [COLOR="green"]res/anim[/COLOR] folder]

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >

<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0.0" />

</set>

[/QUOTE]

CrossFade Effect

[QUOTE]
[B]Java Code[/B] [CrossFadeActivity.java]
package package.name.here;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

public class CrossfadeActivity extends Activity implements AnimationListener {

TextView txtMessage1, txtMessage2;
Button btnStart;

// Animation
Animation animFadeIn, animFadeOut;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crossfade);

txtMessage1 = (TextView) findViewById(R.id.txtMessage1);
txtMessage2 = (TextView) findViewById(R.id.txtMessage2);
btnStart = (Button) findViewById(R.id.btnStart);

// load animations
animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_out);

// set animation listeners
animFadeIn.setAnimationListener(this);
animFadeOut.setAnimationListener(this);

// button click event
btnStart.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// make fade in element visible
txtMessage2.setVisibility(View.VISIBLE);
// start fade in animation
txtMessage2.startAnimation(animFadeIn);

// start fade out animation
txtMessage1.startAnimation(animFadeOut);
}
});

}

@Override
public void onAnimationEnd(Animation animation) {
// Take any action after completing the animation

// if animation is fade out hide them after completing animation
if (animation == animFadeOut) {

// hide faded out element
txtMessage1.setVisibility(View.GONE);
}

if(animation == animFadeIn){
// do something after fade in completed

// set visibility of fade in element
txtMessage2.setVisibility(View.VISIBLE);
}

}

@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}

}

[B]XML Code[/B] [activity_crossfade.xml in [COLOR="green"]res/layout/[/COLOR] folder]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView android:id="@+id/txtMessage1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This text will fade out"
android:layout_centerInParent="true"
android:textSize="25dp"/>

<TextView android:id="@+id/txtMessage2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This text will fade in"
android:layout_centerInParent="true"
android:textSize="25dp"
android:visibility="gone"/>

<Button android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Animation"
android:layout_marginTop="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"/>

</RelativeLayout>

[B]NOTE:No need to create any xml in anim folder for crossfade because it uses fadein and fadeout xml files from anim folder][/B]
[/QUOTE]

FadeIn Effect

[QUOTE]
[B]Java Code[/B] [FadeInActivity.java]

package package.name.here;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class FadeInActivity extends Activity implements AnimationListener {

TextView txtMessage;
Button btnStart;

// Animation
Animation animFadein;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fadein);

txtMessage = (TextView) findViewById(R.id.txtMessage);
btnStart = (Button) findViewById(R.id.btnStart);

// load the animation
animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);

// set animation listener
animFadein.setAnimationListener(this);

// button click event
btnStart.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
txtMessage.setVisibility(View.VISIBLE);

// start the animation
txtMessage.startAnimation(animFadein);
}
});

}

@Override
public void onAnimationEnd(Animation animation) {
// Take any action after completing the animation

// check for fade in animation
if (animation == animFadein) {
Toast.makeText(getApplicationContext(), "Animation Stopped",
Toast.LENGTH_SHORT).show();
}

}

@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}

}

[B]XML Code[/B] [activity_fadein.xml in [COLOR="green"]res/layout/[/COLOR] folder]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView android:id="@+id/txtMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is fadein animation"
android:layout_centerInParent="true"
android:textSize="25dp"
android:visibility="gone"/>

<Button android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Animation"
android:layout_marginTop="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"/>

</RelativeLayout>

[B]XML Code[/B] [fade_in.xml in [COLOR="green"]res/anim/[/COLOR] folder]

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >

<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1.0" />

</set>

[/QUOTE]

Also Check Out Adding Animations To Your Application [Part 2]:victory:

Please correct me if the code goes wrong somewhere.

Saurabh Shah

----------------------------------------------------------------

Hit Thanks :good: