Friday 10 June 2016

Crossfade Animation Android

activity_crossfadeanimation.xml

<?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>

CrossfadeanimationActivity.Java

package com.example.crossfadeanimation;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 CrossfadeanimationActivity 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_crossfadeanimation);

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

}

}
After this create one folder in Resources name as anim. In anim folder  create two one xml files as fadein.xml and fadeout.xml with below code

fadein.xml

<?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>
fadeout.xml

<?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>



No comments:

Post a Comment

Ads Inside Post