Friday 10 June 2016

Fadein Animation in Android

activity_fadein_animation.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/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>

FadeinAnimationActivity.java

package com.example.fadeinanalimation;

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;
import android.widget.Toast;

public class FadeinAnimationActivity 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_animation);

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

}

}
After this create one folder in Resources name as anim. In anim folder  create one one xml file as fadein.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>

No comments:

Post a Comment

Ads Inside Post